版权声明:本文为大都督作者的原创文章,未经 大都督 允许也可以转载,但请注明出处,谢谢! 共勉! https://blog.csdn.net/qq_37335220/article/details/85236490
将文件1中的内容复制到文件2中
package com.example.demo.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @Description Stream类
* 输入流代表从外设流入到计算机内存的数据序列
* 输出流代表从计算机内存流向外设的数据序列
* @author 大都督
* @date 2018年12月24日
*/
public class StreamTest {
public static void main(String[] args) throws IOException {
File file1 = FileTest.createFile();
File file2 = FileTest.createFile(FileTest.parent_1, "copy.txt");
//文件复制
file1CopyToFile2(file1, file2);
}
/**
* @Title: 将文件1中的内容复制到文件2中
* @Description:
* @param file1
* @param file2
* @author 大都督
* @date 2018年12月24日
* @return void
* @throws IOException
*/
private static void file1CopyToFile2(File file1, File file2) throws IOException {
FileInputStream fileInputStream = new FileInputStream(file1);
byte[] b = new byte[(int)file1.length()];
fileInputStream.read(b);
FileOutputStream fileOutputStream = new FileOutputStream(file2);
fileOutputStream.write(b);
fileOutputStream.close();
fileInputStream.close();
}
}