正确地重新组合分割文件

时间:2012-06-17 08:06:12

标签: java file-io io inputstream outputstream

请查看以下代码

package normal;

import java.io.*;
import java.util.*;

public class ReGenerateFiles
{
    private File inputFolder, outputFolder;
    private String outputFormat, nameOfTheFile;

    private FileInputStream fis;
    private FileOutputStream fos;
    List <Byte> al;

    private byte[] buffer;

    private int blockNumber = 1;

    public ReGenerateFiles(File inputFile, File outputFile, String nameOfTheFile, String outputFormat)
    {
        this.inputFolder = inputFile;
        this.outputFolder = outputFile;
        this.nameOfTheFile = nameOfTheFile;
        this.outputFormat = outputFormat;
    }

    public void reGenerate() throws IOException
    {
        File file[] = inputFolder.listFiles();

        for(int i=0;i<file.length;i++)
        {
            System.out.println(file[i]);
        }

        for(int i=0;i<file.length;i++)
        {
            try
            {
                buffer = new byte[5000000];


                int read = fis.read(buffer, 0, buffer.length);
                fis.close();

                writeBlock();


            }
            catch(IOException io)
            {
                io.printStackTrace();
            }
            finally
            {
                fis.close();
                fos.close();
            }
        }
    }

    private void writeBlock()throws IOException
    {
        try
        {
            fos = new FileOutputStream(outputFolder+"/"+nameOfTheFile+"."+outputFormat,true);
                fos.write(buffer, 0, buffer.length);
                fos.flush();
                fos.close();
        }
        catch(Exception e)
        {
            fos.close();
            e.printStackTrace();
        }
        finally
        {
            fos.close();
        }
    }


}

在这里,我正在尝试将拆分文件重新组合回原始文件。 This is how I split it(所选答案)

现在,当我试图重新组装它时,我收到一条错误,上面写着“无法访问kalimba.mp3。它被另一个程序使用”。执行93个分割文件后会发生这种情况(还有200个)。为什么会发生这种情况,即使我确保在最终阻止内部关闭流?

另一个问题,我已经将500000分配为字节数组值。我这样做是因为我无法根据即将处理的特定文件的原始大小设置字节数组。我将字节数组值指定为

buffer = new byte[(byte)file[i].length();

之前,但它没有奏效。

请帮我解决这两个问题,并正确地重新组装拆分文件。

1 个答案:

答案 0 :(得分:0)

这是因为缓冲区从未适合实际内容的大小。它总是50000,实际大小各不相同。那就是问题

相关问题