Results 1 to 8 of 8
  1. #1
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default How to use Scanner ?

    Hi,

    i have used Scanner to read a textfile and input data into a table

    Java Code:
     Scanner scan = new Scanner(new File("textfile.txt"));
    when i run the jar file through Netbeans it works fine, but when i manually go and run this from the My Documents (Where my jar file is generated), it throws an error, i think may be we are suppose to give path, if so how can i assign? and also i wont be knowing the path, it keeps changing, please help me out

    Regards
    Sandeep

  2. #2
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Default

    what type of error you got?
    ThanX
    Rj

  3. #3
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default

    Hi RJ

    i got my error msg what i had told to display.

    All i want is how to set dynamic path for that textfile, so that the application reads that file without any problem

    Sandeep

  4. #4
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Post

    try getAbsolutePath() in File class
    then convert to string and pass the path...
    ThanX
    Rj

  5. #5
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default

    Actually tell me where should we keep the Textfile so that application always reads it? i mean which path it should be placed within the generated file.

    Sandeep

  6. #6
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Post

    the jar file and the text file (you are going to read) always put in the same folder..
    then only the scanner will read the text file.. if you put the txt file in other location it dont work..
    ThanX
    Rj

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

    Default

    Quote Originally Posted by sandeep43 View Post
    Actually tell me where should we keep the Textfile so that application always reads it? i mean which path it should be placed within the generated file.
    Put this near the start of your code and it'll tell you where your application runs and so where you are supposed to put your file:

    Java Code:
    System.out.println(System.getProperty("user.dir"));
    But then again, what should your application do if its working directory changes? I leave that up to you.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    sandeep43 is offline Member
    Join Date
    May 2011
    Posts
    97
    Rep Power
    0

    Default

    hey guys,

    both of u rock, it worked awesome.

    thanks and regards
    Sandeep

Similar Threads

  1. Using scanner for CSV
    By getName() in forum Advanced Java
    Replies: 7
    Last Post: 06-20-2010, 04:33 PM
  2. Help With Scanner
    By jtmoney0511 in forum New To Java
    Replies: 10
    Last Post: 10-12-2009, 11:24 PM
  3. Need help with scanner.
    By mainy in forum New To Java
    Replies: 3
    Last Post: 07-28-2009, 02:11 PM
  4. Scanner
    By choko in forum New To Java
    Replies: 10
    Last Post: 01-24-2009, 03:37 PM
  5. need help with scanner
    By whiterex in forum New To Java
    Replies: 1
    Last Post: 04-22-2008, 01:41 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
  •