org.springframework.util.FileCopyUtils

输入

1
2
3
4
5
6
7
8
9
10
11
12
// 从文件中读入到字节数组中
File file = new File("D:\\project\\intellij-git\\testgit\\src\\main\\webapp\\test-temporary\\test.txt");
byte[] bytes = FileCopyUtils.copyToByteArray(file);
System.out.println(new String(bytes));//123
// 从输入流中读入到字节数组中
InputStream inputStream = new FileInputStream(file);
byte[] bytes1 = FileCopyUtils.copyToByteArray(inputStream);
System.out.println(new String(bytes1));//123
// 从输入流中读入到字符串中
Reader reader = new FileReader(file);
String copyToString = FileCopyUtils.copyToString(reader);
System.out.println(copyToString);//123

  

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 从字节数组到文件
FileCopyUtils.copy("1234".getBytes(), file);
// 从文件到文件
File file2 = new File("D:\\project\\intellij-git\\testgit\\src\\main\\webapp\\test-temporary\\test2.txt");
int copy = FileCopyUtils.copy(file, file2);
System.out.println(copy);//4
// 从字节数组到输出流
OutputStream outputStream = new FileOutputStream(file2);
FileCopyUtils.copy("1234".getBytes(), outputStream);
// 从输入流到输出流
InputStream inputStream2 = new FileInputStream(file);
OutputStream outputStream2 = new FileOutputStream(file2);
int copy2 = FileCopyUtils.copy(inputStream2, outputStream2);
System.out.println(copy2);//4
// 从输入流到输出流
Reader reader1 = new InputStreamReader(inputStream2);
Writer writer = new OutputStreamWriter(outputStream2);
int copy3 = FileCopyUtils.copy(reader1, writer);
System.out.println(copy3);//4
// 从字符串到输出流
FileCopyUtils.copy("12345", writer);

  

posted @   草木物语  阅读(384)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-02-07 java 大小写转换
点击右上角即可分享
微信分享提示