Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

cmdline & java

Status
Not open for further replies.

yogpatel

Programmer
Jan 30, 2010
7
IN
Hello,
I have problem of fetching cmdline output to textfile & passing parameter to cmdline from textfile in java, give me some hint for how to do it? thanx.
regads,
Patel
 
java MyJavaApp > MyTextFile.txt

will send console output from MyJavaApp to MyTextFile.txt

You could use a textfile as a param for you app and then get your app to read the contents.


java MyJavaAppToReadTextFile MyTextFile.txt

 
Is "passing parameter to cmdline from textfile in java" really what you mean?

There are 2 ways to pass information from the commandline to your program, one is very easy, and well for small inputs, but is vulnerable to some special characters, interpreted by the shell.

In linux it would be
Code:
java YourClass $(< someFile)
which wouldn't pass the name of the file, but the content to your program, which would consume it in args:
Code:
public static void main (String[] args)

The second approach is to redirect System.in to read from a file, and redirect that to your program:
Code:
cat someFile | java YourClass
Here you would use from java
Code:
Scanner sc = new Scanner (System.in);
and read from the scanner - alternatively from BufferedReader or something like that.


don't visit my homepage:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top