0

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();
    }
  }
}
YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
rfovaleris
  • 23
  • 1
  • 1
  • 3
  • [`FileUtils#listFiles`](https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#listFiles) from apache commons-io could work –  Jan 10 '16 at 12:00
  • or [`File#listFiles`](https://docs.oracle.com/javase/8/docs/api/java/io/File.html#listFiles-java.io.FilenameFilter-) –  Jan 10 '16 at 12:04

3 Answers3

1

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.

Marco Altieri
  • 3,726
  • 2
  • 33
  • 47
1

If you are using Java 8, then you can do it pretty easily using Files#list. Getting your list of Files starting with FILE_NAME and ending with .001, .002, .003, ... should work like this:

Path path = Paths.get(FILE_NAME);
Pattern pattern = Pattern.compile(Pattern.quote(path.getFileName().toString()) + "\\.\\d{3}");
List<File> list = Files.list(path.getParent())
        .filter(f -> pattern.matcher(f.getFileName().toString()).matches())
        .map(Path::toFile)
        .collect(Collectors.toList());

This is just from the top of my head, I didn't test it as I had no .00x files lying around.

Oromis
  • 347
  • 4
  • 13
1
import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.regex.Pattern;

public class ListFiles {

private static final String BASE_DIR = "<your directory>";
private static final String FILE_EXT = ".*\\.[0-9]{3,3}";

private class FileFilter implements FilenameFilter {

    private String ext;

    public FileFilter(String ext) {

        this.ext = ext;

    }

    public boolean accept(File dir, String name) {

        return Pattern.matches(ext, name) ? true : false;

    }
}

public static void main(String args[]) {

    System.out.println(Arrays.toString(new ListFiles().listFile(BASE_DIR, FILE_EXT)));

}

public String[] listFile(String folder, String ext) {

    File dir = new File(folder);
    String[] list = dir.list(new FileFilter(ext));
    return list;

}

}

Nikhil B
  • 76
  • 3