如下所示:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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 {
|
?
|
1
2
3
4
5
6
7
8
9
|
// 第一种: 使用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();
|
?
|
1
2
3
4
5
6
7
8
9
10
|
// 第二种: 使用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();
|
?
|
1
2
3
4
5
6
7
8
9
|
// 第三种: 使用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();
|
?
|
1
2
3
4
5
6
7
8
9
10
|
// 第四种: 使用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();
|
?
|
1
2
3
4
5
6
7
8
9
10
11
|
// 第五种: 使用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();
|
?
|
1
2
3
4
5
6
7
8
9
|
// 第六种: 使用高效缓冲流,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();
|
?
|
1
2
3
4
5
6
7
8
9
10
|
// 第七种: 使用高效缓冲流,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();
|
?
|
1
2
|
}
}
|
以上这篇基于java文本复制的7种方式总结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。
原文链接:http://blog.csdn.net/qq_26106607/article/details/79123496
相关文章
猜你喜欢
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 77
-
2025-05-29 69
-
2025-05-29 106
-
2025-05-25 79
-
2025-05-25 39
热门评论

