案例研究:这是分割文件的有效方法吗?

时间:2014-02-09 14:10:10

标签: java file-io split

所以我在学校的java课程中通过我的方式工作。为了更好地理解代码应该做什么,引用它:

“(拆分文件)假设您要将一个巨大的文件(例如,一个10 GB的AVI文件)备份到CD-R。您可以通过将文件拆分成较小的部分并分别备份这些部分来实现它编写一个实用程序,使用以下命令将大文件拆分为较小的文件: java ClassName SourceFile numberOfPieces

该命令会创建文件 SourceFile.1 SourceFile2 ...等等

现在要清楚了。这篇文章绝不是试图找到问题的“解决方案”。我解决了它(用我所知道的)。而且我只想更加关注编写代码时遇到的一些问题。

  1. 是否需要为每个文件im创建一个新输出 复制到?这不需要不必要的系统电源吗?
  2. 复制的第一个文件(SourceFile在这种情况下是.png 文件)可以查看。并显示原始的一小部分 图片。 (如果我分成两部分。我可以查看一半的图片。)但是 我无法看到后者......为什么会这样?
  3. 是否可以以任何方式重新组合分割文件?如果我的 图片被分成两个文件,我可以把它们放回原处 查看整个图片?
  4. 代码,如果你想看一下。

    欢迎所有反馈, 祝你有美好的一天! :)

    package oblig2;
    
    import java.io.*;
    import java.util.*;
    
    public class Test {
    
    /**
     * Main method
     * 
     * @param args[0] for source file          
     * @param args[1] for number of pieces           
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
    
        // The program needs to be executed with two parameters in order to
        // work. This sentence check for it.
        if (args.length != 2) {
            System.out.println("Usage: java Copy sourceFile numberOfPieces");
            System.exit(1);
        }
        // Check whether or not the sourcefile exists
        File sourceFile = new File(args[0]);
        if (!sourceFile.exists()) {
            System.out.println("Source file " + args[0] + " does not exist");
            System.exit(2);
        }
        // Need an Array to store all the new files that is supposed to contain
        // parts of the original file
        ArrayList<File> fileArray = new ArrayList<File>();
    
        // All the new files need their own output(or do they?)
        ArrayList<BufferedOutputStream> outputArray = new ArrayList<BufferedOutputStream>();
    
        // Using randomAccessFile on the sourcefile to easier read parts of it
        RandomAccessFile inOutSourceFile = new RandomAccessFile(sourceFile,
                "rw");
    
        // This loop changes the name for the new files, so they match the
        // sourcefile with an appended digit
        for (int i = 0; i < Integer.parseInt(args[1]); i++) {
            String nameAppender = String.valueOf(i);
            String nameBuilder;
            int suffix = args[0].indexOf(".");
            nameBuilder = args[0].substring(0, suffix);
            fileArray.add((new File(nameBuilder + nameAppender + ".dat")));
        }
    
        // Here i create the output needed for all the new files
        for (int i = 0; i < Integer.parseInt(args[1]); i++) {
            outputArray.add(new BufferedOutputStream(new FileOutputStream(
                    new File(fileArray.get(i).getAbsolutePath()))));
        }
    
        // Now i determine in how many parts the sourcefile needs to be split,
        // and the size of each.
        float size = inOutSourceFile.length();
        double parts = Integer.parseInt(args[1]);
        double partSize = size / parts;
        int r, numberOfBytesCopied = 0;
    
    
        // This loop actually does the job of copying the parts into the new
        // files
        for (int i = 1; i <= parts; i++) {
            while (inOutSourceFile.getFilePointer() < partSize * i) {
                r = inOutSourceFile.readByte();
                outputArray.get(i - 1).write((byte) r);
                numberOfBytesCopied++;
            }
    
        }
        // Here i close the input and outputs
        inOutSourceFile.close();
        for (int i = 0; i < parts; i++) {
            outputArray.get(i).close();
        }
    
        // Display the operations
        System.out.println(args[0] + " Has been split into " + args[1]
                + " pieces. " + "\n" + "Each file containig " + partSize
                + " Bytes each.");
    
    }
    
    }
    

1 个答案:

答案 0 :(得分:1)

  1. 当然有必要打开所有输出文件。但是你不必一直打开它们。你可以打开第一个文件,写入它,关闭它,打开第二个文件,写入它,关闭它等等。
  2. 例如,文件格式,.png具有必须遵循的结构。它可能有特殊的标题,并且可能有特殊的页脚。这就是为什么当这个文件分成两个或更多时,第一个将失去它的页脚,中间将失去它的页眉和页脚,最后将失去它的标题。这使得它们无法用作单个文件。
  3. 当然有可能。通过组合所有部分,原始文件填充进行重组。