Search code examples
javafilebufferedreader

Merge contents of file of files starting with a prefix


How to merge files starting with a pattern from a folder in Java.

I have files as below:

UW110_file_1.dat

ROW1   DATA1  SOMESTUFF1
ROW2   DATA2  SOMESTUFF2

UW110_file_2.dat

ROW3   DATA3  SOMESTUFF3
ROW4   DATA4  SOMESTUFF4

UW110_file_2.hdr

ROW3   DATA3  SOMESTUFF3
ROW4   DATA4  SOMESTUFF4

I need to check all files starting with UW110 that has an extension of .dat and merge them.

In this case i'll have to create a file as below

UW110_file

ROW1   DATA1  SOMESTUFF1
ROW2   DATA2  SOMESTUFF2
ROW3   DATA3  SOMESTUFF3
ROW4   DATA4  SOMESTUFF4

Solution

  • Create a File object with the path to where the files are located, let's call it datFolder

    public String getMergedFiles(File datFolder) {
        ArrayList<File> files = new ArrayList<>();
        files.addAll(Arrays.asList(datFolder.listFiles((f) -> f.getName().endsWith(".dat") && f.getName().startsWith("UW110"))));
    
        StringBuilder sb = new StringBuilder();
    
        for (File datFile : files) {
            InputStream is = new FileInputStream(datFile);
            BufferedReader buf = new BufferedReader(new InputStreamReader(is));
    
            String line = buf.readLine();
            while(line != null) {
                sb.append(line).append("\n");
                line = buf.readLine();
            }
    
            buf.close();
            is.close();
        }
    
        return sb.toString();
    }
    
    public void writeToFile(String input, File file) {
        PrintWriter out = new PrintWriter(file);
        try{
            out.println(input);
        }catch(Exception e){
            e.printStackTrace();
        }
        out.close();
    }
    
    // Somewhere in your code
    File folder = new File("myFolder");
    writeToFile(getMergedFiles(folder), new File("outputFile.dat"));
    

    This makes use of a lambda though, look up how they work if you're not using java 8, should be easy turning it into a loop.

    Didn't test this code but i believe it should work. If not, probably a quick fix should do it.

    You can simply write the string this method returns to a file and you should have your final file!

    Enjoy :)