Results 1 to 6 of 6
  1. #1
    cherrychives is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default pass input file to class from console

    At the moment I have a java program which reads a text file and does certain operations with its contents.. how do I change this:


    Java Code:
    scan = new Scanner(new FileReader(inputtextfile.txt))
    ..so that I can give the file to the program directly from the console, like so:

    java myProgram inputtextfile.txt

  2. #2
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: pass input file to class from console

    System.in is stdin, which is where your console input comes from.

    Java Code:
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String input;
    input = in.readLine();

  3. #3
    cherrychives is offline Member
    Join Date
    Apr 2012
    Posts
    25
    Rep Power
    0

    Default Re: pass input file to class from console

    So I cannot use Scanner for this??

  4. #4
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: pass input file to class from console

    Well, I showed you a way to use it, think about how you can take that information and use it with Scanner.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default Re: pass input file to class from console

    Quote Originally Posted by jlczuk View Post
    System.in is stdin, which is where your console input comes from.

    Java Code:
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    String input;
    input = in.readLine();
    They want to supply the filename at teh command line, not read input from Sys.in.

    For the OP...parameters like your filename are what exist in the args parameter on the main() method.
    In your example args[0] would contain the value "inputtextfile.txt", which you can pass into the line creating the Scanner instead of the hardcoded filename.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,335
    Blog Entries
    7
    Rep Power
    20

    Default Re: pass input file to class from console

    Quote Originally Posted by Tolls View Post
    They want to supply the filename at teh command line, not read input from Sys.in.

    For the OP...parameters like your filename are what exist in the args parameter on the main() method.
    In your example args[0] would contain the value "inputtextfile.txt", which you can pass into the line creating the Scanner instead of the hardcoded filename.
    For MS Window you can get away with it by supplying 'CON:' as a file name.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. How to get input from Console
    By karma in forum New To Java
    Replies: 8
    Last Post: 08-13-2010, 09:32 PM
  2. who to take user input ,not by console...
    By Shyam Singh in forum New To Java
    Replies: 3
    Last Post: 06-13-2008, 10:09 PM
  3. how to take input from console in jsp
    By veena in forum New To Java
    Replies: 1
    Last Post: 05-06-2008, 04:39 AM
  4. ways to get input from console
    By soni in forum New To Java
    Replies: 2
    Last Post: 05-05-2008, 09:44 PM
  5. How to read input from console
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-16-2008, 10:57 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
  •