• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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: 24204
44
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 ]
 
Those are the largest trousers in the world! Especially when next to this ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic