shell - executing cat command from java program does not work as expected -


i trying use cat command within java program merge 2 files one. line of code contains cat command takes 2 files file1 , file2 , writes third file called combinedfile. however, observe instead of creating file (combinedfile) , writing it, program merely displays output on terminal.

how can make sure indeed 2 files copied third file.

import java.io.bufferedreader; import java.io.inputstreamreader;  public class executeshellcommand  {      public static void main(string[] args)      {          executeshellcommand obj = new executeshellcommand();          string command = "cat file1 file2 > combinedfile";          string output = obj.executecommand(command);          system.out.println(output);      }      private string executecommand(string command)      {          stringbuffer output = new stringbuffer();          process p;         try          {             p = runtime.getruntime().exec(command);             p.waitfor();             bufferedreader reader = new bufferedreader(new inputstreamreader(p.getinputstream()));              string line = "";                         while ((line = reader.readline())!= null)              {                 output.append(line + "\n");             }          } catch (exception e) {             e.printstacktrace();         }          return output.tostring();      }  } 

edit:

i tried out processbuilder suggested, error. code

import java.io.bufferedreader; import java.io.inputstreamreader; import java.io.*; import java.util.*;  public class executeshellcommand {     public static void main(string[] args)      {         try         {                         processbuilder builder = new processbuilder("cat", "/home/pepperboy/desktop/file1.txt","/home/pepperboy/desktop/file2.txt");             file combinedfile = new file("/home/pepperboy/desktop/file3.txt");             builder.redirectoutput(combinedfile);             builder.redirecterror(combinedfile);             process p = builder.start();         }          catch(ioexception e)         {                 e.printstacktrace();         }      } } 

error

executeshellcommand.java:14: cannot find symbol symbol  : method redirectoutput(java.io.file) location: class java.lang.processbuilder             builder.redirectoutput(combinedfile); 

i found related question. summarize useful things couple answers found there, file redirection requires shell, exec doesn't have shell context. luckily, can execute processes redirection using processbuilder. case, like:

public static void main(string[] args)  {      try{                     processbuilder builder = new processbuilder("cat", "file1","file2");         file combinedfile = new file("combinedfile");         builder.redirectoutput(combinedfile);         builder.redirecterror(combinedfile);         process p = builder.start();     } catch(ioexception e){         //handle exception...     }  } 

note: may receive error when calling redirecterror or redirectoutput stating symbol cannot found. occur if compiling against version of java before 1.7, since 1.7 when these methods introduced. if possible upgrade java, doing eliminate error.

if not possible upgrade java, following code work:

public static void main(string[] args)  {      try{                     processbuilder builder = new processbuilder("cat", "file1","file2");         file combinedfile = new file("combinedfile");         process p = builder.start();          inputstream isfromcat = p.getinputstream();         outputstream oscombinedfile = new fileoutputstream(combinedfile);          byte[] buffer = new byte[1024];         int read = 0;         while((read = isfromcat.read(buffer)) != -1) {             oscombinedfile.write(buffer, 0, read);         }      } catch(ioexception e){         //handle exception...     }  } 

it worthwhile note making system call cat not optimal way combine files java. have been assuming toy case represent more complex use case you. if want combine 2 files, should write code avoid system calls , append files reading both in input streams , writing them out result file. if you're having trouble figuring out, details belong in question.


Comments