欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

基于java文本复制的7种方式总结

时间:2021-02-23 15:14:17|栏目:JAVA代码|点击:

如下所示:

package copy;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FileCopy {
public static void main(String[] args) throws IOException {
// 第一种: 使用FileReader和FileWrite,一次读取一个字符
		FileReader fr = new FileReader("D:\\a.txt");
		FileWriter fw = new FileWriter("D:\\b.txt");
		int ch;
		while((ch = fr.read()) != -1) {
			fw.write(ch);
		}
		fw.close();
		fr.close();
// 第二种: 使用FileReader和FileWrite,一次读取一个字符数组
		FileReader fr = new FileReader("D:\\a.txt");
		FileWriter fw = new FileWriter("D:\\b.txt");
		char[] chs = new char[1024];
		int len;
		while((len = fr.read(chs)) != -1) {
			fw.write(chs, 0, len);
		}
		fw.close();
		fr.close();
// 第三种: 使用FileOutputStream和FileInputStream,一次读取一个字节
		FileInputStream fis = new FileInputStream("D:\\a.txt");
		FileOutputStream fos = new FileOutputStream("D:\\b.txt");
		int ch;
		while((ch = fis.read()) != -1) {
			fos.write(ch);
		}
		fos.close();
		fis.close();
// 第四种: 使用FileOutputStream和FileInputStream,一次读取一个字节数组
		FileInputStream fis = new FileInputStream("D:\\a.txt");
		FileOutputStream fos = new FileOutputStream("D:\\b.txt");
		int ch;
		byte[] by = new byte[1024];
		while((ch = fis.read(by)) != -1) {
			fos.write(by, 0, ch);
		}
		fos.close();
		fis.close();
// 第五种: 使用BufferedReader和BufferedWriter,一次读取一行
		BufferedReader br = new BufferedReader(new FileReader("D:\\a.txt"));
		BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\b.txt"));
		String line;
		while((line = br.readLine()) != null) {
			bw.write(line);
			bw.newLine();
			bw.flush();
		}
		bw.close();
		br.close();
// 第六种: 使用高效缓冲流,BufferedInputStream和BufferedOutputStream,一次读取一个字节
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.txt"));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\b.txt"));
		int ch;
		while((ch = bis.read()) != -1) {
			bos.write(ch);
		}
		bos.close();
		bis.close();
// 第七种: 使用高效缓冲流,BufferedInputStream和BufferedOutputStream,一次读取一个字节数组
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.txt"));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\b.txt"));
		int ch;
		byte[] by = new byte[1024];
		while((ch = bis.read(by)) != -1) {
			bos.write(by, 0, ch);
		}
		bos.close();
		bis.close();
}
}

上一篇:Java synchronize底层实现原理及优化

栏    目:JAVA代码

下一篇:java根据扩展名获取系统图标和文件图标示例

本文标题:基于java文本复制的7种方式总结

本文地址:http://www.codeinn.net/misctech/68211.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有