文件拷贝

    xiaoxiao2026-08-15  5

    package cn.mldn.demo; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; public class CopyDemo { public static void main(String[] args)throws Exception{ long start=System.currentTimeMillis(); if(args.length!=2){ System.out.println(“参数输入不正确”); System.exit(1); } File infile=new File(args[0]); if(!infile.exists()){//源文件不存在要加! System.out.println(“未到源文件”); System.exit(1); } File outfile=new File(args[1]); if(!outfile.getParentFile().exists()){ System.out.println(“不存在此目录,但己自动创建”); outfile.mkdir(); } InputStream input=new FileInputStream(infile); OutputStream output=new FileOutputStream(outfile); int temp=0;//保存每次读取的次数 byte date[]=new byte[1024];//每次循环所存的数据最大为1024字节 while((temp=input.read(date))!=-1){//每次循环所读取的数据都存到数组中(缓冲区) output.write(date,0,temp);//输出数组中的数据至指定文件,从每次循环开始,由数组起始位置到读取位置 } input.close(); output.close(); long end=System.currentTimeMillis(); System.out.println((start-end)/60.0000/60.0000+”秒”); } }

    转载请注明原文地址: https://ju.6miu.com/read-1311213.html
    最新回复(0)