useruser123 useruser123 - 2 months ago 17
Java Question

How to merge one file to another \ Linux

I am trying to append one text file to another by using linux commands from my Java program. I am completely new to Linux. I tried sorting and it works just fine, so I have no idea what I am doing wrong with using 'cat'.
Could you please review my code and help me figure out what I am doing wrong.

public static void mergeRecords(String fileName, String overflowFileName)
String command = "cat " + overflowFileName + " >> " + fileName;
try {
Process r = Runtime.getRuntime().exec(command);
} catch (Exception e) {
throw new RuntimeException(e);

fge fge

Runtime#exec is not a shell.

This is a very common misconception. What you need to do is:

  • create a Process with the command cat file1 file2,
  • take the output of that process,
  • dump that output into a file.

Hint: use a ProcessBuilder, this will make your job much easier.