编程语言
735
数据源:"C:\Users\你是小朱老师呀\Desktop\test.txt" 数据的目的地: "C:\Users\你是小朱老师呀\Desktop\XSC\test.txt" 实现步骤: 1.创建源文件与目标文件 2.创建节点流 3.创建缓冲流 4.读取、写入 5.释放
package person.xsc.praticeIII; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Copy2 { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub long start = System.currentTimeMillis(); String srcPath="C:\\Users\\你是小朱老师呀\\Desktop\\JAVA编程.DOC"; String destPath="C:\\Users\\你是小朱老师呀\\Desktop\\XSC\\test4.DOCX"; //1.造源文件与目标文件 File srcFile = new File(srcPath); File destFile = new File(destPath); //2.造节点流 FileInputStream fis = new FileInputStream((srcFile)); FileOutputStream fos = new FileOutputStream(destFile); //3.造缓冲流 BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(fos); //4.读取、写入 byte [] tmp = new byte[1024]; int temp = 0 ; try{ while((temp=bis.read(tmp))!=-1){ // 开始拷贝 bos.write(tmp,0,temp) ; // 边读边写 } System.out.println("拷贝完成!") ; }catch(IOException e){ e.printStackTrace() ; System.out.println("拷贝失败!") ; } //5.关闭流:关闭外层流的同时,内层流也会自动的进行关闭。 bos.close(); bis.close(); long end = System.currentTimeMillis(); System.out.println("复制文件内容共耗时:"+(end-start)+"毫秒"); } } 输出: 拷贝完成! 复制文件内容共耗时:5毫秒
广告