简体   繁体   中英

Merging all .txt files in a folder using Java.IO

I am trying to merge all .txt files in one specific folder and create an output.txt file. I am new to Java learning Java.IO package. Here is my program which compiles fine and creates an output file but does not write anything to it. I verified my input text files and it has data.

import java.io.*;

class  Filemerger
{
    public static void main(String[] args) throws IOException
    {
        PrintWriter pw = new PrintWriter("true1.txt");      
        File f = new File("E:\\A");
        String[] s = f.list();
        for (String s1 : s)
        {
            File f1 = new File(f, s1);
            BufferedReader br = new BufferedReader(new FileReader(f1));
            String line = br.readLine();
            while (line != null)
            {
                pw.println(line);
                line = br.readLine();
            }
        }
        pw.flush();
        pw.close();
    }
}

I think your error is minimal, maybe some paths are wrong or stuff like that. Anyway you should use the new IO API called NIO for interacting with files. The key classes are Paths and Files located in the package java.nio .

class  Filemerger
{
    public static void main(String[] args) throws IOException
    {
        Path output = Paths.get("true1.txt");
        Path directory = Paths.get("E:\\A");
        Stream<Path> filesToProcess = Files.list(directory);

        // Iterate all files
        filesToProcess.forEach(path -> {
            // Get all lines of that file
            Stream<String> lines = Files.lines(path);
            // Iterate all lines
            lines.forEach(line -> {
                // Append the line separator
                String lineToWrite = line + System.lineSeparator();

                // Write every line to the output file
                Files.write(output, lineToWrite.getBytes(StandardCharsets.UTF_8));
            });
        });
    }
}

Or you could also use the Stream#flatMap method if you just want to collect all lines from all files without a mapping to which file they belong:

Path output = Paths.get("true1.txt");
Path directory = Paths.get("E:\\A");

Files.list(directory)
    .flatMap(Files::lines)
    .forEach(line -> {
        Files.write(output, (line + System.lineSeparator())
            .getBytes(StandardCharsets.UTF_8));
    });

Note that Files#write and other methods also accept a bunch of options that you can set. For example if the file should get created if it does not exist yet. Or, if it does already exist, whether content should be appended or old content should be deleted instead.

Therefore take a look at the documentation pages:

Your code was not writing anything to the file. Perhaps println() method. Please adjust the path of input and output file as per your requirements.

With slight modification:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

public class MergeTextFiles {
    public static void main(String[] args) throws IOException {
        File f = new File("./src/main/resources");

        File merged = new File("./src/main/resources/merged.txt");
        if (!merged.exists()) {
            merged.createNewFile();
        }

        PrintWriter pw = new PrintWriter(merged);

        String[] s = f.list();
        System.out.println("list of files-> " + Arrays.asList(s));

        for (String s1 : s) {
            File f1 = new File(f, s1);
            BufferedReader br = new BufferedReader(new FileReader(f1));

            String line = "";
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                pw.println(line);
            }
        }
        pw.flush();
        pw.close();
    }
}

Update: It was not 'println'.

You should always handle exceptions in the main method, otherwise exceptions are ignored.

if the directory doesn't exist, f.list() returns null (!);

If the directory contains subdirectories, the name of those subdirectories will also be returned, so you must test that a file indeed is a file;

When you open a file, you should close it or (better) use a try block with resources:

public static void main(String[] args)
{
    try(PrintWriter pw = new PrintWriter("true1.txt")) {
        File f = new File("E:\\A");
        String[] s = f.list();
        if (s == null) {
            throw new IOException("Directory doesn't exist: " + f);
        }
        for (String s1 : s)
        {
            File f1 = new File(f, s1);
            if (!f1.isFile()) {
                continue;
            }
            try (Reader reader = new FileReader(f1);
                    BufferedReader br = new BufferedReader(reader)) {
                String line = br.readLine();
                while (line != null)
                {
                    pw.println(line);
                    line = br.readLine();
                }
            }
        }
    } catch (Throwable e) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM