数据源:"C:\Users\你是小朱老师呀\Desktop\test.txt" 数据的目的地: "C:\Users\你是小朱老师呀\Desktop\XSC\test.txt" 实现步骤: 1.创建源文件与目标文件 2.创建节点流 3.创建缓冲流 4.读取、写入 5.释放
1package person.xsc.praticeIII; 2 3import java.io.BufferedInputStream; 4import java.io.BufferedOutputStream; 5import java.io.ByteArrayOutputStream; 6import java.io.File; 7import java.io.FileInputStream; 8import java.io.FileNotFoundException; 9import java.io.FileOutputStream; 10import java.io.IOException; 11import java.io.InputStream; 12import java.io.OutputStream; 13 14public class Copy2 { 15 public static void main(String[] args) throws IOException { 16 // TODO Auto-generated method stub 17 long start = System.currentTimeMillis(); 18 String srcPath="C:\\Users\\你是小朱老师呀\\Desktop\\JAVA编程.DOC"; 19 String destPath="C:\\Users\\你是小朱老师呀\\Desktop\\XSC\\test4.DOCX"; 20 //1.造源文件与目标文件 21 File srcFile = new File(srcPath); 22 File destFile = new File(destPath); 23 //2.造节点流 24 FileInputStream fis = new FileInputStream((srcFile)); 25 FileOutputStream fos = new FileOutputStream(destFile); 26 //3.造缓冲流 27 BufferedInputStream bis = new BufferedInputStream(fis); 28 BufferedOutputStream bos = new BufferedOutputStream(fos); 29 //4.读取、写入 30 byte [] tmp = new byte[1024]; 31 int temp = 0 ; 32 try{ 33 while((temp=bis.read(tmp))!=-1){ // 开始拷贝 34 bos.write(tmp,0,temp) ; // 边读边写 35 } 36 System.out.println("拷贝完成!") ; 37 }catch(IOException e){ 38 e.printStackTrace() ; 39 System.out.println("拷贝失败!") ; 40 } 41 //5.关闭流:关闭外层流的同时,内层流也会自动的进行关闭。 42 bos.close(); 43 bis.close(); 44 long end = System.currentTimeMillis(); 45 System.out.println("复制文件内容共耗时:"+(end-start)+"毫秒"); 46 } 47 48} 49输出: 50拷贝完成! 51复制文件内容共耗时:5毫秒 52
