3

Please have a look at the following code

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();
        }
    }


}

In here, I am trying to re-assemble a splited file, back to its original file. This is how I split it (The selected answer)

Now, when I am trying to re-assemble it, I am getting an error saying something similar to "Cannot access kalimba.mp3. It is used by another program". This happens after executing 93 splitted files (there are 200 more). Why it is happening, even though I have make sure the streams are closed inside finally block?

Another question, I have assigned 500000 as the byte array value. I did this because I failed to set the byte array according to the original size of the particular file which is about to process. I assigned the byte array value as

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

before, but it didn't work.

Please help me to solve these two issues and re-assemble the splitted file back, correctly.

Community
  • 1
  • 1
PeakGen
  • 21,894
  • 86
  • 261
  • 463

1 Answers1

0

That is because the buffer never fitted to the size of the actual content. It is always 50000, and the actually size vary. Thats the issue

PeakGen
  • 21,894
  • 86
  • 261
  • 463