美文网首页Java 杂谈
综合实战--文件拷贝

综合实战--文件拷贝

作者: 落在牛背上的鸟 | 来源:发表于2018-07-21 15:43 被阅读1次

    在DOS系统中,文件拷贝的命令:copy 源文件 目标文件路径
    如果要实现文件的拷贝操作,有两种方法:

    方法一: 将所有的文件内容一次性读取到程序中,然后一次输出;

    • 需要开辟一个与文件大小一样的数组,但是如果文件大小过大,程序可能会崩溃;

    方法二: 采用边读边输出的方式,就不会占用过大的内存空间;

    现在需要拷贝二进制数据,使用字节流比较合适。

    设置启动参数:

    设置启动参数
    范例: 代码的基本实现
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    /**
     * @author liuwq
     *
     */
    public class CopyTest {
        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("源文件不存在");
            }
            File outFile = new File(args[1]);
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            
            //实现文件内容的拷贝
            InputStream input = new FileInputStream(inFile);
            OutputStream output = new FileOutputStream(outFile);
            
            int temp = 0;
            while ((temp = input.read()) != -1) {
                output.write(temp);
            }
            
            input.close();
            output.close();
            
            long end = System.currentTimeMillis();
            System.out.println("拷贝花费的时间:" + (end - start));
        }
    }
    
    //执行结果
     拷贝花费的时间:175402
    

    以上的程序属于单字节拷贝,每一次读取一个字节,所以循环耗费的时间太长;如果改变以上代码的性能问题,可以使用部分数据读取并保存的方式,回顾字节流的两个操作方法:

    • InputStream: public int read(byte[] b) throws IOException
      |- 将内容读取到字节数组中,如果没有数据则返回 -1 ,
    • OutputStream: public void write(byte[] b, int off, int len) throws IOException
      |- 要设置的字节数组实际上就是read()方法里面使用的数组;
      |- 一定是从字节数组的第0个元素开始输出,输出读取的数据长度。

    范例:改进拷贝操作

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    public class CopyTestVersion_2 {
        public static void main(String[] args) throws IOException{
            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("源文件不存在");
            }
            File outFile = new File(args[1]);
            if (!outFile.getParentFile().exists()) {
                outFile.getParentFile().mkdirs();
            }
            
            //实现文件内容的拷贝
            InputStream input = new FileInputStream(inFile);
            OutputStream output = new FileOutputStream(outFile);
            
            //实现文件拷贝
            int temp = 0;
            byte[] data = new byte[1024];
            while ((temp = input.read(data)) != -1) {
                output.write(data, 0, temp);
            }
            
            input.close();
            output.close();
            
            long end = System.currentTimeMillis();
            System.out.println("拷贝花费的时间:" + (end - start));
        }
    }
    

    相关文章

      网友评论

        本文标题:综合实战--文件拷贝

        本文链接:https://www.haomeiwen.com/subject/qarhmftx.html