org.springframework.util.FileCopyUtils

输入

// 从文件中读入到字节数组中
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

  

输出

// 从字节数组到文件
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 @ 2022-02-07 16:13  草木物语  阅读(408)  评论(0)    收藏  举报