把一个文件复制一份
public class CopyFileDemo1 {
//需求:把一个文件复制一份 原理:读取已有文件 写入另一文件
public static void main(String[] args) throws IOException {
//1.明确源文件和目的文件
File srcFile = new File("e:\\templatefile\\file.txt");
File destFile = new File("e:\\templatefile\\copy_file.txt");
//2.创建输入流和原文件相关 输出流和目的文件相关
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
//3.使用输入流读取字节 输出流写入目的文件
int ch = 0;
while((ch=fis.read())!= -1){
fos.write(ch);
}
//4.关闭资源
fis.close();
fos.close();
}
}