Why does this work? beginning question
From: ComieCreep (Mike97739_at_msn.com)
Date: 12/30/04
- Next message: ComieCreep: "test"
- Previous message: IchBin: "Re: When a tab has been seleted..?"
- Next in thread: VisionSet: "Re: Why does this work? beginning question"
- Reply: VisionSet: "Re: Why does this work? beginning question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 30 Dec 2004 14:04:19 -0800
Hi,
The following code works. The question I have concerns closing a file.
The line:
if (dis !=null)
seems to say if dis is not empty. So the file should shut down after the
first line is read. I know I am misunderstanding this.
here's the code. Check out the last 5 lines or so
import java.io.*;
class FileReadTest {
public static void main (String[] args)
{
FileReadTest f = new FileReadTest();
f.readMyFile();
}
void readMyFile() {
DataInputStream dis = null;
String record = null;
int recCount = 0;
try {
File f = new File("D:/testfile2");
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while ( (record=dis.readLine()) != null ) {
recCount++;
System.out.println(recCount + ": " + record);
}
} catch (IOException e) {
// catch io errors from FileInputStream or readLine()
System.out.println("Uh oh, got an IOException error!" +
e.getMessage());
} finally {
// if the file opened okay, make sure we close it
if (dis != null) {
try {
dis.close();
} catch (IOException ioe) {
}
}
}
}
}
Thanks,
mike
- Next message: ComieCreep: "test"
- Previous message: IchBin: "Re: When a tab has been seleted..?"
- Next in thread: VisionSet: "Re: Why does this work? beginning question"
- Reply: VisionSet: "Re: Why does this work? beginning question"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]