Search code examples
javafilesplitter

File Merging in Java with .00x extension


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

Solution

  • You can implement a FileFilter and pass it to the method File.listFiles() as shown below:

    import java.io.File;
    import java.io.FileFilter;
    import java.util.Arrays;
    import java.util.List;
    
    
    public class Test {
        public static void main(String[] args) {
            final String FILE_NAME = "testfile";
            /* The method listFiles returns all the files in the path 
               (I used "." to select the working directory).
               This method accept a FileFilter as parameter. The file filter
               decides what files to return. It is a simple interface with just
               one method. */
            File[] fileList = new File(".").listFiles(new FileFilter() {
    
                @Override
                public boolean accept(File pathname) {
                    /* Return true to include the file in the list */
                    return pathname.getName().startsWith(FILE_NAME);
                }
            });
    
            List<File> list = Arrays.asList(fileList);
    
            for (File f: list) {
                System.out.println(f);
            }
        }
    }
    

    If you do not like to work with anonymous classes, you can just implement your FileFilter as a public class in its own file.