文件读写以及ftp上传下载

这篇博客介绍了如何使用Java进行文件的读写操作,包括读取和写入文件,解决了中文字符乱码的问题。同时,文章还提供了FTP上传和下载文件的示例代码,包括设置工作目录、文件类型等关键步骤。
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">最近在文件读写方面用的比较多,也学到了不少东西。以前对文件读写比较陌生,一直感觉很难,实际接触之后,使用起来其实并不难下面提供我的几个工具类</span>
<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">文件的读、写</span></span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="html">package xxx

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import com.ultrapower.project.web.bean.agent_conf;

public class FileRead {
	/**
     * 一行一行读取文件,适合字符读取,若读取中文字符时会出现乱码
     * 
     * 流的关闭顺序:先打开的后关,后打开的先关,
     *       否则有可能出现java.io.IOException: Stream closed异常
     * 
     * @throws IOException 
     */
    public void readFile01() throws IOException {
        FileReader fr=new FileReader("E:/phsftp/evdokey/evdokey_201103221556.txt");
        BufferedReader br=new BufferedReader(fr);
        String line="";
        //String[] arrs=null;
        while ((line=br.readLine())!=null) {
            //arrs=line.split(",");
            //System.out.println(arrs[0] + " : " + arrs[1] + " : " + arrs[2]);
        	System.out.println(line);
        }
        br.close();
        fr.close();
    }
    
    /**
     * 一行一行读取文件,解决读取中文字符时出现乱码
     * 
     * 流的关闭顺序:先打开的后关,后打开的先关,
     *       否则有可能出现java.io.IOException: Stream closed异常
     * 
     * @throws IOException 
     */
    public List<agent_conf> readFile02(String agentName) throws IOException {
    	//FileInputStream fis=new FileInputStream("E:/phsftp/evdokey/evdokey_201103221556.txt");
    	
    	FileInputStream fis=new FileInputStream(new File("d:/"+agentName+"/conf/client.txt"));
    	
        InputStreamReader isr=new InputStreamReader(fis, "UTF-8");
        BufferedReader br = new BufferedReader(isr);
        //简写如下
        //BufferedReader br = new BufferedReader(new InputStreamReader(
        //        new FileInputStream("E:/phsftp/evdokey/evdokey_201103221556.txt"), "UTF-8"));
        String line="";
        List<agent_conf> conf=new ArrayList<agent_conf>();
        while ((line=br.readLine())!=null) {
        	agent_conf c=new agent_conf();
        	c.setKey(line);
        	conf.add(c);
//        	System.out.println(line);
        }
        br.close();
        isr.close();
        fis.close();
        return conf;
    }
    
    /**
     * 一行一行写入文件,适合字符写入,若写入中文字符时会出现乱码
     * 
     * 流的关闭顺序:先打开的后关,后打开的先关,
     *       否则有可能出现java.io.IOException: Stream closed异常
     * 
     * @throws IOException 
     */
    public void writeFile01(String agentName) throws IOException {
        String[] arrs={
            "zhangsan,23,FuJian",
            "lisi,30,ShangHai",
            "wangwu,43,BeiJing",
            "laolin,21,ChongQing",
            "ximenqing,67,GuiZhou"
        };
        FileWriter fw=new FileWriter(new File("d:/"+agentName+"/conf/client.txt"));
        //写入中文字符时会出现乱码
        BufferedWriter  bw=new BufferedWriter(fw);
        //BufferedWriter  bw=new BufferedWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("E:/phsftp/evdokey/evdokey_201103221556.txt")), "UTF-8")));

        for(String arr:arrs){
            bw.write(arr+"\t\n");
        }
        bw.close();
        fw.close();
    }
    
    /**
     * 一行一行写入文件,解决写入中文字符时出现乱码
     * 
     * 流的关闭顺序:先打开的后关,后打开的先关,
     *       否则有可能出现java.io.IOException: Stream closed异常
     * 
     * @throws IOException 
     */
    public void writeFile02(String datas,String agentName) throws IOException {
        //写入中文字符时解决中文乱码问题
        FileOutputStream fos=new FileOutputStream(new File("d:/"+agentName+"/conf/client.txt"));
        OutputStreamWriter osw=new OutputStreamWriter(fos, "UTF-8");
        BufferedWriter  bw=new BufferedWriter(osw);
        bw.write(datas);
        //注意关闭的先后顺序,先打开的后关闭,后打开的先关闭
        bw.close();
        osw.close();
        fos.close();
    }
    public static void main(String[] args) throws IOException {
    	FileRead fr=new FileRead();
//    	fr.readFile02("apache-flume-10.2");
    	String datas="sssasd";
    	fr.writeFile02(datas, "apache-flume-10.2");
	}
}
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>
文件的上传、下载 我用的是ftp进行对端的上传下载 具体东西自己修改下 就可以使用了

<span style="font-family:Arial, Helvetica, sans-serif;"><span style="background-color: rgb(255, 255, 255);">依赖包 :</span></span><pre name="code" class="html"><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> commons-net-1.4.1.jar PFTClinet.jar</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">
</span>

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">package x'x'x;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
/**
 * JAVA FTP上传下载 助手类
 * 
 * commons-net-1.4.1.jar PFTClinet.jar
 * 
 * @author : zs
 */
public class FtpManage {


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * FTP上传单个文件测试
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static void fileUploadByFtp(String agentName) {
<span style="white-space:pre">		</span>FTPClient ftpClient = new FTPClient();
<span style="white-space:pre">		</span>FileInputStream fis = null;
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>ftpClient.connect("192.168.0.114");
<span style="white-space:pre">			</span>ftpClient.login("wang", "wang");
<span style="white-space:pre">			</span>File srcFile = new File("d:/"+agentName+"/conf/client.txt");
<span style="white-space:pre">			</span>fis = new FileInputStream(srcFile);
<span style="white-space:pre">			</span>// 设置上传目录
<span style="white-space:pre">			</span>ftpClient.changeWorkingDirectory("/agent/"+agentName+"/conf");
<span style="white-space:pre">			</span>ftpClient.setBufferSize(1024);
<span style="white-space:pre">			</span>ftpClient.setControlEncoding("GBK");
<span style="white-space:pre">			</span>// 设置文件类型(二进制)
<span style="white-space:pre">			</span>ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
<span style="white-space:pre">			</span>//设置上传文件名
<span style="white-space:pre">			</span>ftpClient.storeFile("client.conf", fis);
<span style="white-space:pre">			</span>//如果上传成功则清空当前本地文件夹
//<span style="white-space:pre">			</span>File dFile=new File("d:/"+agentName+"/conf");
//<span style="white-space:pre">			</span>srcFile.delete();
<span style="white-space:pre">		</span>} catch (IOException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">			</span>throw new RuntimeException("FTP客户端出错!", e);
<span style="white-space:pre">		</span>} finally {
<span style="white-space:pre">			</span>IOUtils.closeQuietly(fis);
<span style="white-space:pre">			</span>try {
<span style="white-space:pre">				</span>ftpClient.disconnect();
<span style="white-space:pre">			</span>} catch (IOException e) {
<span style="white-space:pre">				</span>e.printStackTrace();
<span style="white-space:pre">				</span>throw new RuntimeException("关闭FTP连接发生异常!", e);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * FTP下载单个文件测试
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static void fileDownloadByFtp(String agentName) {
<span style="white-space:pre">		</span>FTPClient ftpClient = new FTPClient();
<span style="white-space:pre">		</span>FileOutputStream fos = null;
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>ftpClient.connect("192.168.0.114");
<span style="white-space:pre">			</span>ftpClient.login("wang", "wang");
<span style="white-space:pre">			</span>//linux目录
<span style="white-space:pre">			</span>String remoteFileName = "/agent/"+agentName+"/conf/client.conf";
<span style="white-space:pre">			</span>// fos = new FileOutputStream("E:/test/test_back_081901.sql");
<span style="white-space:pre">			</span>//之后要进行字符串的替换 agent名
<span style="white-space:pre">			</span>//将文件下载好的文件流写到本地d盘下
<span style="white-space:pre">			</span>String downPath="d:/"+agentName+"/conf/client.txt";
<span style="white-space:pre">			</span>//判断文件路径是否存在,不存在则创建
<span style="white-space:pre">			</span>File path1 = new File("d:/"+agentName+"/conf");
<span style="white-space:pre">			</span>if (!path1.exists()) {
<span style="white-space:pre">				</span>path1.mkdirs();
<span style="white-space:pre">			</span>}
<span style="white-space:pre">			</span>fos = new FileOutputStream(downPath);
<span style="white-space:pre">			</span>
<span style="white-space:pre">			</span>ftpClient.setBufferSize(1024);
<span style="white-space:pre">			</span>// 设置文件类型(二进制)
<span style="white-space:pre">			</span>ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
<span style="white-space:pre">			</span>ftpClient.retrieveFile(remoteFileName, fos);
<span style="white-space:pre">		</span>} catch (IOException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">			</span>throw new RuntimeException("FTP客户端出错!", e);
<span style="white-space:pre">		</span>} finally {
<span style="white-space:pre">			</span>IOUtils.closeQuietly(fos);
<span style="white-space:pre">			</span>try {
<span style="white-space:pre">				</span>ftpClient.disconnect();
<span style="white-space:pre">			</span>} catch (IOException e) {
<span style="white-space:pre">				</span>e.printStackTrace();
<span style="white-space:pre">				</span>throw new RuntimeException("关闭FTP连接发生异常!", e);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>public static void main(String[] args){
<span style="white-space:pre">		</span>fileUploadByFtp("apache-flume-10.2");
//<span style="white-space:pre">		</span>fileDownloadByFtp("apache-flume-10.2");
<span style="white-space:pre">		</span>System.out.println("成功!");
<span style="white-space:pre">	</span>}
}

</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值