Results 1 to 6 of 6
Thread: command line arguments
- 03-28-2010, 04:32 AM #1
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
command line arguments
srry if this isn't a newbie question, in that case i posted in wrong section
i understand how to work the arguement passed in when u do java myjavaapp
my question is how can i get it to read a text file
example
i have text file
// superman.txt
one two three four five
// end of text file
how can i pass the whole txt file into my command line
java myjavaapp < superman.txt
this doesn't work, i just get output as <
when i do System.out.print(args[0]);
is this possible, i am successful in passing just regular string
but i want to pass a whole .txt
and i am aware i can do this with bufferReader etc......
but i dont' want that =)
thx for your thoughts
-
Perhaps this is what you want: The ins and outs of standard input/output - JavaWorld
Thank God for Google!
- 03-28-2010, 04:51 AM #3
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Lines from a text file aren't passed as arguments to a program when you use < or > or | -- those operators redirect the standard input and standard output streams. Here's a very rough example:
That program could be run like so:Java Code:import java.util.Scanner; public class StdinReader { /** * @param args */ public static void main(String[] args) { Scanner stdin = new Scanner(System.in); while (stdin.hasNextLine()) { System.out.println("We received >>" + stdin.nextLine() + "<<"); } } }
Hope that helps.Java Code:host:~ user$ java StdinReader <superman.txt We received >><< We received >><< We received >>one two three four five<< We received >><< We received >><< We received >><<
-Gary-
- 03-28-2010, 04:52 AM #4
Member
- Join Date
- Mar 2010
- Posts
- 8
- Rep Power
- 0
thx for reply but i do not want to do it this way
i know how to use system.in
buffer reader
readLine ..... etcc...
i was wondering if it's possible to pass .txt file as argument to java's main method
and print it off
public class Test{
public static void main(String[] args)
{
for ( int i = 0; i < args.length() ; i++)
{
System.out.println(args[i]);
}
}
}Last edited by evermore; 03-28-2010 at 04:54 AM.
- 03-28-2010, 05:05 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Depending on the operating system you might be able to do what you want from the command shell:
That won't work for Windows unless you install the GNU findutils. findutils - GNU Project - Free Software Foundation (FSF)Java Code:host:~ user$ cat superman.txt | xargs java YourClass
Otherwise you don't get the file contents, just the file name. Of course you could write another helper program to read the file and pass arguments to your main().
-Gary-
- 03-28-2010, 05:08 AM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Similar Threads
-
command line arguments
By jttslg in forum Advanced JavaReplies: 23Last Post: 03-24-2010, 05:06 PM -
Command-Line Arguments
By mustachMan in forum New To JavaReplies: 5Last Post: 02-26-2010, 03:13 AM -
Command Line Arguments
By Nakira in forum NetBeansReplies: 10Last Post: 02-04-2010, 03:45 PM -
arguments in command-line
By girlet18 in forum New To JavaReplies: 2Last Post: 01-21-2010, 02:05 PM -
Command line arguments help
By may88 in forum New To JavaReplies: 8Last Post: 12-08-2009, 01:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks