[prog] Java I/O problem 2
Noir
acknak_halflife at yahoo.co.uk
Sun Apr 17 23:40:25 EST 2005
--- Noir <acknak_halflife at yahoo.co.uk> wrote:
> I have a plain-text file and it reads
> Tony 12.33 45.2 33.33 33.22
> Jessica 11.3 33.3 12.33 45.5
> Now, I have to read the file and give the output
> like
>
> Tony
>
> 12.33
> 12.33
> 45.2
> 33.33
> 33.22
>
> Jessica
> ...
> ...
>
>
> I can read the file and get the output (STDOUT). But
> how am I going to break it down into new lines?
My code looks like this:
// FileReadTest.java
// Copyright 1998 DevDaily Interactive, Inc. All
Rights Reserved.
import java.io.*;
class FileReadTest {
//--------------------------------------------------<
main >--------//
public static void main (String[] args) {
FileReadTest t = new FileReadTest();
t.readMyFile();
}
//--------------------------------------------<
readMyFile >--------//
void readMyFile() {
String record = null;
int recCount = 0;
try {
FileReader fr = new FileReader("mydata.txt");
BufferedReader br = new BufferedReader(fr);
record = new String();
while ((record = br.readLine()) != null) {
// recCount++;
// System.out.println(recCount + ": " +
record);
System.out.println(record + "\n");
}
} catch (IOException e) {
// catch possible io errors from readLine()
System.out.println("Uh oh, got an
IOException error!");
e.printStackTrace();
}
} // end of readMyFile()
} // end of class
Send instant messages to your online friends http://uk.messenger.yahoo.com
More information about the Programming
mailing list