Results 1 to 4 of 4
- 11-24-2008, 06:15 PM #1
Member
- Join Date
- Nov 2008
- Location
- England
- Posts
- 2
- Rep Power
- 0
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!
- 11-24-2008, 06:32 PM #2
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
I'd say yes because I did the exact same thing with BufferedWriter so it should work both ways.Java Code:BufferedReader br = new BufferedReader(new InputStreamReader(userFile), 1);
- 11-24-2008, 07:02 PM #3
Member
- Join Date
- Nov 2008
- Location
- England
- Posts
- 2
- Rep Power
- 0
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!
- 11-25-2008, 01:21 AM #4
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
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:
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.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(); }Neil Coffey
Javamex - Java tutorials and performance info
Similar Threads
-
Java file reader...?
By prabhurangan in forum New To JavaReplies: 3Last Post: 11-21-2008, 08:19 AM -
[SOLVED] Need help with file reader
By syed.shuvo in forum New To JavaReplies: 6Last Post: 09-27-2008, 07:43 PM -
[SOLVED] reader and writer on same file handle
By Nicholas Jordan in forum Advanced JavaReplies: 11Last Post: 07-01-2008, 03:39 AM -
[SOLVED] File chooser selecting file from directory...?
By prabhurangan in forum AWT / SwingReplies: 12Last Post: 06-18-2008, 04:08 AM -
help with file reader
By jason27131 in forum New To JavaReplies: 1Last Post: 08-01-2007, 03:03 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks