Results 1 to 4 of 4
  1. #1
    myazuid is offline Member
    Join Date
    Nov 2008
    Location
    England
    Posts
    2
    Rep Power
    0

    Default Selecting an input file for Reader

    Hello,

    I am trying to write a scanner, with the eventual aim of creating a parser to follow on from it. So I am trying to read a text file, look at each character one by one in order to decide what it is. My issue at the moment is how to allow to the user to select the file they wish to scan. Just a few questions:

    1. Is the BufferedReader the correct reader to use?

    2. If I am to use this, is it ok to simply limit the size of the buffer to one byte, will this ensure I am scanning one character at a time?

    3. Can you ask a bufferedReader to read a variable? (which will be the filename of the text file that the user has asked for) i.e. Ask the user what file they want, store it as a variable called userFile and then have:

    BufferedReader br = new BufferedReader(new InputStreamReader(userFile), 1);

    Many thanks in advance for any help!

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    I'm a little confused on a few things. When you say look at each character one by one in order to decide what it is. Do you mean decide if it's a character, int, etc or is it supposed to be like some variable already defined?

    You might be able to attach a delimiter to your reader to only read 1 character at a time.

    as for doing
    Java Code:
    BufferedReader br = new BufferedReader(new InputStreamReader(userFile), 1);
    I'd say yes because I did the exact same thing with BufferedWriter so it should work both ways.

  3. #3
    myazuid is offline Member
    Join Date
    Nov 2008
    Location
    England
    Posts
    2
    Rep Power
    0

    Default

    I have, since my post, spoken to a friend who has cleared some things up for me. I was going about it in the wrong way, apparently a char array is a better way, so you read in the entire line and then split it into characters to work out where tokens are and remove junk.

    thank you for your help!

  4. #4
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    If you need to read in line by line and then split each line at certain characters, the easiest way is probably to use BufferedReader.readLine() as you were doing, then use the String.split() method. For example, if your tokens are space-delimited:

    Java Code:
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(f), "ISO-8859-1"));
    try {
      String line;
      while ((line = br.readLine()) != null) {
      
        String[] toks = line.split(" +");
        for (String tok : toks) {
          // do something with 'tok'
        }
    
      }
    } finally {
      br.close();
    }
    You pass a regular expression into String.split(). So " +" means "one or more spaces". You could also use "\\s+" for "one or more instances of whitespace". See the Pattern class for more details.

Similar Threads

  1. Java file reader...?
    By prabhurangan in forum New To Java
    Replies: 3
    Last Post: 11-21-2008, 09:19 AM
  2. [SOLVED] Need help with file reader
    By syed.shuvo in forum New To Java
    Replies: 6
    Last Post: 09-27-2008, 08:43 PM
  3. [SOLVED] reader and writer on same file handle
    By Nicholas Jordan in forum Advanced Java
    Replies: 11
    Last Post: 07-01-2008, 04:39 AM
  4. [SOLVED] File chooser selecting file from directory...?
    By prabhurangan in forum AWT / Swing
    Replies: 12
    Last Post: 06-18-2008, 05:08 AM
  5. help with file reader
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 04:03 AM

Posting Permissions

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