• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic