• Post Reply Bookmark Topic Watch Topic
  • New Topic

Facing problem --exec("cat>sample.txt")  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody.
I'm facing some problem while executing the following program.......


import java.io.*;
public class InterpretProgram
{
public static String readStream(InputStream stream)
{
String output = "";
try
{
BufferedReader input = new BufferedReader(new InputStreamReader(stream));
String temp = "";
while ((temp = input.readLine()) != null)
output += temp + "\n";
input.close();
}
catch (IOException ie)
{
}
return(output);
}
public static void main(String[] args)
{
try
{

Process p = Runtime.getRuntime().exec("cat>sample.txt");//is this possible.

System.out.println("Output: " + readStream(p.getInputStream()));
System.out.println("Errors: " + readStream(p.getErrorStream()));
}
catch (IOException ie)
{ }
}
}

If not how can i use exec() to execute the command cat>sample.txt


Thanks in advance
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, "cat > sample.txt" will truncate sample.txt to zero length, then copy standard input to the file. You're using it as though you expect it to emit the contents of the file; that would be with a "<" rather than a ">". Furthermore, you could (and would!) just omit the "<", since not only is it not necessary, it won't work with Runtime.exec() (for reasons that I don't need to explain here.)

In any case: why aren't just just opening the file with a FileReader ? This will be way more efficient than running an external program to read the contents of the file!
[ February 27, 2006: Message edited by: Ernest Friedman-Hill ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!