• 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.

n00b Java question concerning file reading

xospec1alk

Diamond Member
alrite i want my java program to read a text file line by line.

for example if the contents of the file were

something
or
other

it would take the 'something' and return it to a variable. then it would take 'or' and do the same thing, and then 'other' and i have no clue how many lines there will be, so whenever it detects enf of file i suppose.

any hlep would be appreciated

 
You're talking about java.io which has some major revisions in JDK 1.4. I'm sure you can find some online resources, but if you want to be up to date, make sure the info reflects the changes in 1.4. Ivor Horton's Beginning Java 2 (for JDK 1.4) has a few good chapters on reading and writing files.
 
This has nothing to do with new I/O (which has more to do with writing high performance network servers).

He just needs a BufferedReader that wraps a FileReader, and to call readLine() on the reader. Or InputStreams are okay if you don't need Unicode. I.e.

BufferedReader in = new BufferedReader(new FileReader("/tmp/data.txt"));
String s;

while ( (s = in.readLine()) != null) {
// Process s
}

As you can see, readLine() returns null at end-of-file. You'll have to put everything in a try/catch block.
 
Originally posted by: manly
This has nothing to do with new I/O (which has more to do with writing high performance network servers).

he might as well learn the NIO instead of old IO. if its not worth doing right, its not worth doing. 🙂
 
Originally posted by: wfay
Originally posted by: manly
This has nothing to do with new I/O (which has more to do with writing high performance network servers).

he might as well learn the NIO instead of old IO. if its not worth doing right, its not worth doing. 🙂
No, because unless I'm mistaken java.io is not being deprecated, nor does it have any serious problems besides performance scalability in certain situations (hence the creation of New I/O). Use it if it's the right API for the job.

It would be a different matter if I was advising him to use AWT with the *old* event model.
 
Back
Top