• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Reading a file in java

SJP0tato

Senior member
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.
 
BufferedReader br = new BufferedReader(new InputStreamReader(filename));
String s = br.readLine();

or something like that. it's been awhile
 
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.
 
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.

 
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.

 
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!
 
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 ?
 
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. 😛

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.
 
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...
 
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 😉
 
Back
Top