Jan 5, 2011 03:05 AM
(This post was last modified: Jan 5, 2011 03:05 AM by Knautschtabak.)
Hi Psycho,
thats a nice tutorial. But I see one litte problem in the code for file input and file output.
You should close the reader within a finally-clause to get shure that it will also be closed after an exception.
Man, I had really trouble with this in a bigger project because someone didn't close streams like this. Some files couldn't be deleted sometimes and it took a while to figure out that open streams where blocking them.
This may not be a problem in a little program which stops running after the exception occured. But such things are forgotten soon, if one doesn't it right everytime.
This is a solution:
Deque
thats a nice tutorial. But I see one litte problem in the code for file input and file output.
Code
try
{
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while(line != null)
{
// display
System.out.println(line);
// get next line
line = br.readLine();
}
br.close();
}
catch(Exception e)
{
System.out.println(e);
}Man, I had really trouble with this in a bigger project because someone didn't close streams like this. Some files couldn't be deleted sometimes and it took a while to figure out that open streams where blocking them.
This may not be a problem in a little program which stops running after the exception occured. But such things are forgotten soon, if one doesn't it right everytime.
This is a solution:
Code
FileReader fr;
BufferedReader br = null;
try {
fr = new FileReader(fileName);
br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
// display
System.out.println(line);
// get next line
line = br.readLine();
}
} catch (Exception e) {
System.out.println(e);
} finally{
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}

