package FileTest.Stream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
/**
* Created by Mr.Wang on 2019/2/19.
*/
public class CopyFileTest {
public static void main(String[] args) throws Exception{
copyFile("I:\\StudySource\\text.txt","I:\\StudySource\\text2.txt");
}
public static void copyFile(String srcFile, String destFile) throws Exception{
File file1 = new File(srcFile); //源文件
File file2 = new File(destFile); //目标文件
if (!file1.exists()){ //判断源文件是否存在
throw new Exception("源文件不存在");
}
if(!file2.exists()){ //判断目标文件是否存在
file2.createNewFile();
}
FileInputStream fileInputStream = new FileInputStream(srcFile); //文件输入流
FileOutputStream fileOutputStream = new FileOutputStream(destFile); //文件输出流
byte[] bytes = new byte[8*1024]; //单词读入的字节数组
int end = bytes.length;
while ((end = fileInputStream.read(bytes,0,end)) != -1){ //fileInputStream.read(bytes,0,end) 输入流将多个字节读入到数组中,返回读入的字节数
fileOutputStream.write(bytes, 0, end); //输出流将数组中的字节读出到目标文件
}
fileInputStream.close(); //关闭流
fileOutputStream.close();
}
}