Reading a file in java

SJP0tato

Senior member
Aug 19, 2004
267
0
76
Hey guys, if I want to read a string of characters (say letters or numbers) from a file one at a time, and have them assigned individually to a global "String" variable, what's the best/easiest way to do this?

I've tried google-ing for ways of reading from a file, but none of the examples I've used want to compile correctly, or the ones that will compile assign the input character an integer value when I'd like to keep it a char or string of length 1 if possible.

If I'm overlooking something really stupid (very possible) as to why I'm not able to get my file to be read correctly please let me know. Generally what I'm trying to do is read the string from the file one character at a time, and will then take action depending on what that character evaluates to.

Thanks for any help/suggestions,

Ryan P.
 

Daishiki

Golden Member
Nov 9, 2001
1,943
36
91
BufferedReader br = new BufferedReader(new InputStreamReader(filename));
String s = br.readLine();

or something like that. it's been awhile
 

SJP0tato

Senior member
Aug 19, 2004
267
0
76
Originally posted by: xUCIxDaiSHi
BufferedReader br = new BufferedReader(new InputStreamReader(filename));
String s = br.readLine();

or something like that. it's been awhile

This is the one I've been trying to get to work, but at compile-time I get an error about an "unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown".

In order to setup the exception I need to have it try{get input} then catch afterwards? I've never used the exception handler before, I've read about it on the sun webpage, but I wasn't able to get it to work correctly for me. I'm thinking it should be something like

Try{
BufferedReader in = new BufferedReader(new FileReader("blah.txt"));
String s = br.read();
} catch(something goes here???)

To read in a single character & put it in string s?

Thanks guys, I have a feeling I'm really close now.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Originally posted by: SJP0tato
Originally posted by: xUCIxDaiSHi
BufferedReader br = new BufferedReader(new InputStreamReader(filename));
String s = br.readLine();

or something like that. it's been awhile

This is the one I've been trying to get to work, but at compile-time I get an error about an "unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown".

In order to setup the exception I need to have it try{get input} then catch afterwards? I've never used the exception handler before, I've read about it on the sun webpage, but I wasn't able to get it to work correctly for me. I'm thinking it should be something like

Try{
BufferedReader in = new BufferedReader(new FileReader("blah.txt"));
String s = br.read();
} catch(something goes here???)

To read in a single character & put it in string s?

Thanks guys, I have a feeling I'm really close now.

You've got the idea.

 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: MrChad
Originally posted by: SJP0tato
Originally posted by: xUCIxDaiSHi
BufferedReader br = new BufferedReader(new InputStreamReader(filename));
String s = br.readLine();

or something like that. it's been awhile

This is the one I've been trying to get to work, but at compile-time I get an error about an "unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown".

In order to setup the exception I need to have it try{get input} then catch afterwards? I've never used the exception handler before, I've read about it on the sun webpage, but I wasn't able to get it to work correctly for me. I'm thinking it should be something like

Try{
BufferedReader in = new BufferedReader(new FileReader("blah.txt"));
String s = br.read();
} catch(something goes here???)

To read in a single character & put it in string s?

Thanks guys, I have a feeling I'm really close now.

You've got the idea.

If you haven't already found it. Should work, but I haven't tried it.

 

SJP0tato

Senior member
Aug 19, 2004
267
0
76
Aha, got it. Thanks guys!

Now I just need to break it up into 1-character sized bites...shouldn't be too difficult I think. One more quick question: what would be the easiest way to delete a single character from the front of a string (basically I'm going to copy the 1st character in the string somewhere else, then I want to remove the 1st element from the original string so when I return to it I'll remove the next in line).

I was thinking of creating another string and copying all but the 1st character into it, but that seems terribly in-efficient.

You guys rock!
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: SJP0tato
Aha, got it. Thanks guys!

Now I just need to break it up into 1-character sized bites...shouldn't be too difficult I think. One more quick question: what would be the easiest way to delete a single character from the front of a string (basically I'm going to copy the 1st character in the string somewhere else, then I want to remove the 1st element from the original string so when I return to it I'll remove the next in line).

I was thinking of creating another string and copying all but the 1st character into it, but that seems terribly in-efficient.

You guys rock!
A queue of bytes would ?
 

manly

Lifer
Jan 25, 2000
13,591
4,240
136
Originally posted by: amdfanboy

If you haven't already found it. Should work, but I haven't tried it.
(Depending on the file size) at least use a StringBuffer. :p
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: manly
Originally posted by: amdfanboy

If you haven't already found it. Should work, but I haven't tried it.
(Depending on the file size) at least use a StringBuffer. :p

Yeah, I even had that there, I just thought it would complicate it too much for him. But yes, I agree.

BTW, have you seen the new non-tread-safe StringBuilder? It should be even faster because of that.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Thanks for the StringBuilder tip afb. You'd think, if the two have the same interface, that they'd try to unify them under some interface or base class or something. Not that it matters much...
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: kamper
Thanks for the StringBuilder tip afb. You'd think, if the two have the same interface, that they'd try to unify them under some interface or base class or something. Not that it matters much...

Hey, it is Sun ;)