I am currently practicing with splitting and merging a file. I found a piece of code on the web authored by a "krishna" with split and merge classes. The splitter worked like a charm, and I did some modifications to make it work the way I like it.
Here's the catch: I want the merger class to open the .00x files the splitter generates. But it is only limited to exactly eight .00x files, no more no less.
If only I could make it read all .00x files in the folder. I've been thinking long for a solution but I can't seem to generate one. I thought about making something that will scan the number of file with the .00x extension and make a loop based on it. Pls help me, or at least give me hints. Thank you! The code follows:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class MergeFile {
private static String FILE_NAME = JOptionPane.showInputDialog("Enter the file name");
public static void main(String[] args) {
File ofile = new File(FILE_NAME);
FileOutputStream fos;
FileInputStream fis;
short[] fileBytes;
int bytesRead = 0;
List<File> list = new ArrayList<File>();
list.add(new File(FILE_NAME+".001"));
list.add(new File(FILE_NAME+".002"));
list.add(new File(FILE_NAME+".003"));
list.add(new File(FILE_NAME+".004"));
list.add(new File(FILE_NAME+".005"));
list.add(new File(FILE_NAME+".006"));
list.add(new File(FILE_NAME+".007"));
list.add(new File(FILE_NAME+".008"));
try {
fos = new FileOutputStream(ofile,true);
for (File file : list) {
fis = new FileInputStream(file);
fileBytes = new byte[(int) file.length()];
bytesRead = fis.read(fileBytes, 0,(int) file.length());
assert(bytesRead == fileBytes.length);
assert(bytesRead == (int) file.length());
fos.write(fileBytes);
fos.flush();
fileBytes = null;
fis.close();
fis = null;
}
fos.close();
fos = null;
}catch (Exception exception){
exception.printStackTrace();
}
}
}