文件使用字节缓冲区复制,略微增加了目标文件的文件大小,在Java

时间:2018-05-29 19:29:09

标签: java file

我正在创建一个简单的文件复制应用程序供我个人使用。但是当我运行程序时,输出文件略大于源文件。我正在使用缓冲区,具体取决于文件大小,如下所示

  1. 如果文件大小小于9 KB而不是缓冲区是512字节。
  2. 如果文件大小小于9 MB而不是缓冲区大小为512 * 1024字节(512 KB)。
  3. 如果文件大小小于99 MB而不是缓冲区是1024 * 1024字节(1 MB)。
  4. 其他方面的缓冲区大小为2 * 1024 * 1024字节(约2.5 MB)。
  5. 源代码

        import java.io.File;
        import java.io.FileInputStream;
        import java.io.FileOutputStream;
        import java.text.DecimalFormat;
    
        /**
        * mcopy
        */
    public class mcopy {
    
    private static byte buffersm[] = new byte[512]; // buffer for upto 9kb
    private static byte buffermd[] = new byte[512 * 1024]; // buffer for upto 9 MB
    private static byte bufferlg[] = new byte[1024 * 1024]; // buffer for upto 99MB
    private static byte bufferxl[] = new byte[2 * 1024 * 1024]; // buffer for above 100MB
    private static FileInputStream fin = null;
    private static FileOutputStream fout = null;
    private static int i = 0;
    private static double length = 0;
    private static double j = 0;
    private static DecimalFormat decimal_p_ = new DecimalFormat("#.#");
    
    private static void copysm(File a, File b) {
        try {
            length = a.length();
            fin = new FileInputStream(a);
            fout = new FileOutputStream(b);
            i = fin.read(buffersm);
            j = 0;
            while (i != -1) {
                j += i;
                fout.write(buffersm);
                System.out.print("Copying... " + decimal_p_.format(((j / length) * 100)) + " " + a.getName() + " to "
                        + b.getPath() + " buffer: " + buffersm.length + " \r");
                i = fin.read(buffersm);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                fin.close();
                fout.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    private static void copymd(File a, File b) {
        try {
            length = a.length();
            fin = new FileInputStream(a);
            fout = new FileOutputStream(b);
            i = fin.read(buffermd);
            j = 0;
            while (i != -1) {
                j += i;
                fout.write(buffermd);
                System.out.print("Copying... " + decimal_p_.format(((j / length) * 100)) + " " + a.getName() + " to "
                        + b.getPath() + " buffer: " + buffermd.length + " \r");
                i = fin.read(buffermd);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                fin.close();
                fout.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    private static void copylg(File a, File b) {
        try {
            length = a.length();
            fin = new FileInputStream(a);
            fout = new FileOutputStream(b);
            i = fin.read(bufferlg);
            j = 0;
            while (i != -1) {
                j += i;
                fout.write(bufferlg);
                System.out.print("Copying... " + decimal_p_.format(((j / length) * 100)) + " " + a.getName() + " to "
                        + b.getPath() + " buffer: " + bufferlg.length  + " \r");
                i = fin.read(bufferlg);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                fin.close();
                fout.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    private static void copyxl(File a, File b) {
        try {
            length = a.length();
            fin = new FileInputStream(a);
            fout = new FileOutputStream(b);
            i = fin.read(bufferxl);
            j = 0;
            while (i != -1) {
                j += i;
                fout.write(bufferxl);
                System.out.print("Copying... " + decimal_p_.format(((j / length) * 100)) + " " + a.getName() + " to "
                        + b.getPath() + " buffer: " + bufferxl.length  + " \r");
                i = fin.read(bufferxl);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                fin.close();
                fout.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) {
        File in = new File(args[0]);
        File out = new File(args[1]);
        double ld = in.length();
        double l = ld / 1024 / 1024;
        if (l <= ((9 * 1024) / 1024 / 1024)) {
            copysm(in, out);
        } else if (l <= 9) {
            copymd(in, out);
        } else if (l <= 99) {
            copylg(in, out);
        } else {
            copyxl(in, out);
        }
        System.out.println();
    }
    

    }

    文件大小比较....:
    源文件:“龙珠剧集84.mp4”.....文件大小:8,59,93,580字节
    目标文件:“sample.mp4”.....文件szie:8,70,31,808字节

    Command used to run the program

    Comparison between the files used and named in this problem

1 个答案:

答案 0 :(得分:0)

这几乎可以做你现在正在做的事情,但只复制读取的内容,而不是缓冲区的全部内容。基本上主要的区别是使用FileOutputStream.write(byte[], offset, len)而不是FileOutputStream.write(byte[])

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.DecimalFormat;

/**
* mcopy
*/
public class mcopy {
    private static DecimalFormat format = new DecimalFormat("#.#");
    private final static int SMALL = 512;
    private final static int MEDIUM = 512 * 1024;
    private final static int LARGE = 1024 * 1024;
    private final static int XLARGE = 2 * 1024 * 1024;


    private static void copy(File a, File b, byte[] buffer) {
        FileInputStream fin = null;
        FileOutputStream fout = null;

        try {
            double j = 0;
            double length = a.length();
            fin = new FileInputStream(a);
            fout = new FileOutputStream(b);

            int i;
            while(-1 != (i = fin.read(buffer))) {
                j += i;
                fout.write(buffer, 0, i);
                System.out.print("Copying... " + format.format(((j / length) * 100)) + " " + a.getName() + " to "
                    + b.getPath() + " buffer: " + buffer.length + " \r");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                fin.close();
                fout.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        File in = new File(args[0]);
        File out = new File(args[1]);
        double ld = in.length();
        double l = ld / 1024 / 1024;

        byte[] buffer = null;

        if (l <= ((9 * 1024) / 1024 / 1024)) {
            buffer = new byte[SMALL];
        } else if (l <= 9) {
            buffer = new byte[MEDIUM];
        } else if (l <= 99) {
            buffer = new byte[LARGE];
        } else {
            buffer = new byte[XLARGE];
        }
        copy(in, out, buffer);
        System.out.println();
    }
}