1 . 演示文件的copy
package com.yx.demo1;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;import org.junit.Test;/** 本类用于演示复制文件*/
public class TestCopyFile {@Testpublic void testOne()throws Exception{File oldFile = new File("src\\test.txt");//原文件File newFile = new File("src\\test_bak.txt");//副本文件if(!newFile.exists()){newFile.createNewFile();}FileInputStream fis = new FileInputStream(oldFile);//跟原文件建立输入流,从原文件中读取数据。FileOutputStream fos = new FileOutputStream(newFile);//跟副本文件建立输出流,把从原文件中读取的数据写到副本文件中。byte[] data = new byte[1024];int length;//while((length = fis.read(data)) != -1){//fos.write(data, 0, length)//}while(true){length = fis.read(data);if(length == -1){break;}fos.write(data, 0, length);//从数组的第一位置开始,去读到的元素有多长,就会写出多长,为了最后一次读取的时候的精确性。}fis.close();fos.close();}
}