Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating a number of .txt files in Java [closed]

I have a number of .txt files. I would like to concatenate those and generate a text file.
How would I do it in Java?


Following is the case

file1.txt file2.txt 

Concatenation results into

file3.txt

Such that the contents of file1.txt is followed by file2.txt.

like image 524
thetna Avatar asked May 20 '12 17:05

thetna


3 Answers

Using Apache Commons IO

You could use the Apache Commons IO library. This has the FileUtils class.

// Files to read
File file1 = new File("file1.txt");
File file2 = new File("file2.txt");

// File to write
File file3 = new File("file3.txt");

// Read the file as string
String file1Str = FileUtils.readFileToString(file1);
String file2Str = FileUtils.readFileToString(file2);

// Write the file
FileUtils.write(file3, file1Str);
FileUtils.write(file3, file2Str, true); // true for append

There are also other methods in this class that could help accomplish the task in a more optimal way (eg using streams or lists).

Using Java 7+

If you are using Java 7+

public static void main(String[] args) throws Exception {
    // Input files
    List<Path> inputs = Arrays.asList(
            Paths.get("file1.txt"),
            Paths.get("file2.txt")
    );

    // Output file
    Path output = Paths.get("file3.txt");

    // Charset for read and write
    Charset charset = StandardCharsets.UTF_8;

    // Join files (lines)
    for (Path path : inputs) {
        List<String> lines = Files.readAllLines(path, charset);
        Files.write(output, lines, charset, StandardOpenOption.CREATE,
                StandardOpenOption.APPEND);
    }
}
like image 52
Paul Vargas Avatar answered Oct 27 '22 13:10

Paul Vargas


Read file-by-file and write them to target file. Something like the following:

    OutputStream out = new FileOutputStream(outFile);
    byte[] buf = new byte[n];
    for (String file : files) {
        InputStream in = new FileInputStream(file);
        int b = 0;
        while ( (b = in.read(buf)) >= 0)
            out.write(buf, 0, b);
        in.close();
    }
    out.close();
like image 44
AlexR Avatar answered Oct 27 '22 13:10

AlexR


this works fine for me.

// open file input stream to the first file file2.txt
InputStream in = new FileInputStream("file1.txt");
byte[] buffer = new byte[1 << 20];  // loads 1 MB of the file
// open file output stream to which files will be concatenated. 
OutputStream os = new FileOutputStream(new File("file3.txt"), true);
int count;
// read entire file1.txt and write it to file3.txt
while ((count = in.read(buffer)) != -1) {
    os.write(buffer, 0, count);
    os.flush();
}
in.close();
// open file input stream to the second file, file2.txt
in = new FileInputStream("file2.txt");
// read entire file2.txt and write it to file3.txt
while ((count = in.read(buffer)) != -1) {
    os.write(buffer, 0, count);
    os.flush();
}
in.close();
os.close();
like image 21
Samer Makary Avatar answered Oct 27 '22 13:10

Samer Makary