Results 1 to 6 of 6
  1. #1
    evermore is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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:
    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() + "<<");
    		}
    	}
    
    }
    That program could be run like so:
    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 >><<
    Hope that helps.

    -Gary-

  4. #4
    evermore is offline Member
    Join Date
    Mar 2010
    Posts
    8
    Rep Power
    0

    Default

    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.

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Depending on the operating system you might be able to do what you want from the command shell:
    Java Code:
    host:~ user$ cat superman.txt | xargs java YourClass
    That won't work for Windows unless you install the GNU findutils. findutils - GNU Project - Free Software Foundation (FSF)

    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-

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    And I just wanted to point out that none of this is about Java, really. It's really more about how operating systems work.

    -Gary-

Similar Threads

  1. command line arguments
    By jttslg in forum Advanced Java
    Replies: 23
    Last Post: 03-24-2010, 05:06 PM
  2. Command-Line Arguments
    By mustachMan in forum New To Java
    Replies: 5
    Last Post: 02-26-2010, 03:13 AM
  3. Command Line Arguments
    By Nakira in forum NetBeans
    Replies: 10
    Last Post: 02-04-2010, 03:45 PM
  4. arguments in command-line
    By girlet18 in forum New To Java
    Replies: 2
    Last Post: 01-21-2010, 02:05 PM
  5. Command line arguments help
    By may88 in forum New To Java
    Replies: 8
    Last Post: 12-08-2009, 01:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •