How to solve failure to close a FileStream - java

I have closed file steam in try/finally, but code analysis warns me:
Possible failure to close a FileOutputStream
Possible failure to close a PrintWriter
Possible failure to close an OutputStreamWriter
How can failure happen? How can I ensure the FileStream is closed?
public void writeFile(String filepath)
{
BufferedWriter bw = null;
PrintWriter pw = null;
try {
File file = new File(filepath);
bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
pw = new PrintWriter(bfw);
//do something
}catch(Exception e){
e.printStackTrace();
}
finally{
try{
bfw.close();
pw.close();
}catch(Exception e){
e.printStackTrace();
}
}
}

If you are using Java-7 and above then you can use try with resources
File file = new File(filepath);
try(BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
PrintWriter pw = new PrintWriter(bfw);)
{
...
}
catch(Exception exception) //This is optional
{
exception.printStackTrace();
}
You can use catch and finally blocks with try-with-resources statement just like an ordinary try statement.
Hope this helps!

How can failure happen?
See your finally block:
finally{
try{
bfw.close(); <== exception occured here
pw.close(); <== this is not execute
}catch(Exception e){
e.printStackTrace();
}
}
What if an exception occurs in bfw.close()? pw.close() will never execute. And this leads to a resource leak.
How can I ensure the FileStream is closed?
Someone already pointed out using try/catch/finally inside finally.
But if you don't like to see so many try catch finally I would suggest you to use a library like Apache Commons IO.
Solution:
try {
........
} finally {
IOUtils.closeQuietly(bfw);
IOUtils.closeQuietly(pw);
}
And yes, you always have try-with-resources if using Java 7 or above.

If exception happens when closing bw you will not close pw. Try this:
finally{
try{
bw.close();
} catch(Exception e){
e.printStackTrace();
} finally {
pw.close();
}
}

Related

Is there any usage for nested try if without catch?

Came across the below code from a Java book
public void writeFile(String fileName, String content){
File file = new File(fileName);
try {
try (PrintWriter output = new PrintWriter(new FileWriter(file))) {
output.println(content);
output.println();
output.println("End of writing");
}
System.out.println("File been written successfully");
} catch (IOException ex) {
ex.printStackTrace(System.out);
}
}
Nothing is wrong with the above code, I simply couldn't see the point of having a nested try that doesn't define an inner catch block. Or is there any purpose of doing so in which I've missed it?
Revised code:
public void writeFile(String fileName, String content){
File file = new File(fileName);
try (PrintWriter output = new PrintWriter(new FileWriter(file))) {
output.println(content);
output.println();
output.println("End of writing");
System.out.println("File been written successfully");
} catch (IOException ex) {
ex.printStackTrace(System.out);
}
}
The inner try is a try-with-resources:
try (PrintWriter output = new PrintWriter(new FileWriter(file)))
it means, that it manages the resource - PrintWriter - opens it and closes it after every statement in this try is conducted. The outer try is used to catch the error.
Your revised code what Petter Friberg proposed, is equivalent.

Exception - printStackTrace(Printwriter s) method

I would like to make you think about a tiny problem using the method printStackTrace(PrintWriter s). I need to use it in append mode.
The following example is explaining what I mean:
try {
} catch (Exception e) {
try {
e.printStackTrace(new PrintWriter(new FileWriter("mylog.txt", true)));
} catch (IOException ioe) {
System.out.println("I can't open the file mylog.txt");
}
}
Note that
new FileWriter("mylog.txt", true);
is the way I open the file (and create it the first time because it doesn't exist) in append mode.
The result is that in the file there is only the last exception and not a series of exceptions. One time it occurred that the method opened the file in which it didn't write anything.
How can I solve this problem?
Thank you.
Adding to what krzyk mentioned
Per OutputStreamWriter.close() : Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.
As mentioned, if you do not call close and this try{}catch is getting fired frequently, you are not flushing content to file.
It should written like
try {
} catch (Exception e) {
try {
FileWriter fw = new FileWriter("mylog.txt", true)
e.printStackTrace(new PrintWriter(fw));
fw.close();
} catch (IOException ioe) {
System.out.println("I can't open the file mylog.txt");
}
}
A better approach will be
FileWriter fw = new FileWriter("mylog.txt", true);
PrintWriter pw = new PrintWriter(fw);
try {
} catch (Exception e) {
try {
e.printStackTrace(pw);
} catch (IOException ioe) {
System.out.println("I can't open the file mylog.txt");
}
}finally {
pw.close();
fw.close();
}
You should close the created Writers, not closing it might cause the problems you describe.
try (PrintWriter writer = new PrintWriter(new FileWriter("mylog.txt", true))) {
e.printStackTrace(writer);
}

Is closing resources in try block accepted?

A beginners book on Java has the following code in it. This book also explains about exceptions very well, and since I understood how exception works, I got a question about the following code.
For some reason if FileWriter class throws an exception, writer.close() wouldn't be executed. Therefore I think the best place to close the writer object is in a finally block. Even prior to this I have seen many code written like this, where the resource will be closed in the try block itself. I think there is no point in doing so. Only when there is no exception the resource will be closed.
Am I wrong? What is the best way to close resources in java. Should we never write code like the following?
public static void main(String[] args) {
try{
FileWriter writer = new FileWriter("file.txt");
writer.write("i am writing");
writer.close();
}catch(IOException e){
ex.printStackTrace();
}
}
I agree with #cyber-rookie, it is probably best to close resources in a finally block.
Java 7 introduced "try-with-resources" in order to cut down on programming mistakes...
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
You can now write...
try (FileWriter writer = new FileWriter("file.txt")) {
writer.write("i am writing");
} catch (IOException e) {
e.printStackTrace();
}
The compiler will add the extra code to close the writer at the end of the block for you
If your are using Java 7, the best approach is to use try with resource. See https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
try (FileWriter writer = new FileWriter("file.txt")) {
writer.write("i am writing");
}
you are right, resource should be closed in finally block.
as of java 7 you can use try-with-resource also as :
try (BufferedReader br =
new BufferedReader(new FileReader(path))) {
return br.readLine();
}
Because the BufferedReader instance is declared in a try-with-resource statement, it will be closed regardless of whether the try statement completes normally or abruptly if these resources implements AutoCloseable interface.
In my experience we would utilise the finally clause of a try-catch:
public static void main(String[] args) {
FileWriter writer = null;
try {
writer = new FileWriter("file.txt");
writer.write("i am writing");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (writer != null)
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
PS: Place this in separate method and throw the exception, let the class using this handle exceptions.
Answer to a comment to add multiple resources in a try with resource block:
try(FileWriter writer = new FileWriter("file.txt"); BufferedReader reader = new BufferedReader(new FileReader("file.txt"))){
// you can put many AUTOCLOSEABLE objects in try with resource. Just seperate them with ";"
} catch (IOException e) {
e.printStackTrace();
}
At work (Java 6), we close the resources in the TRY block then also do a defensive close in the FINALLY block.
BufferedReader bufferedReader;
try {
//initialize and do something with the bufferedReader
bufferedReader.close();
} catch (FileNotFoundException ex) {
// notify the user
} catch (IOException ex) {
// notify the user
} finally {
if (bufferedReader != null) {
try {
//defensive close
bufferedReader.close();
} catch (IOException ex) {
// this will be thrown if bufferedReader is already closed (in Try block, so can be leave to blank
}
}
}

How to resolve Improper Resource Shutdown or Release issue

I submitted my code for Veraocode Security Testing tool and i got this Improper Resource Shutdown or Release at the below code:
//This function is used to print trace in the in the LogFile for debugging purpose
PrintWriter f;
try {
f = new PrintWriter(new BufferedWriter(new FileWriter(fileName,true)));//issue at this line
synchronized(this) {
f.println(new StringBuffer(dateString).append("<").append(Thread.currentThread().getName()).append(">").append(traceLog).toString());
}
f.close();
checkFileSize();
}catch(IOException e){
e.printStackTrace();
}
Someone please help me on this...
You need to close your PrintWriter.
f.close();
try {
f = new PrintWriter(new BufferedWriter(new FileWriter(fileName,true)));//issue at this line
synchronized(this) {
f.println(new StringBuffer(dateString).append("<").append(Thread.currentThread().getName()).append(">").append(traceLog).toString());
}
checkFileSize();
}catch(IOException e){
e.printStackTrace();
} finally {
if (f != null) {
f.close();
}
}
Resources should be closed in finally clause. So that they definitely get executed. Because if you put closing code in try, and some exception occurs before the closing line. The resource doesn't get closed properly.
Also if you are using JDK-7, have a look at try-with-resources.
You need to close the PrintWriter in catch block also Or create finally block and close the resource.
}catch(IOException e){
e.printStackTrace();
f.close();
}
OR
finally {
if (f != null) {
f.close();
}
}

Deleting file contents android

I need to delete the contents of a file, before I write more information into it. I've tried different ways, such as where I delete the content but the file stays the same size, and when I start writing in it after the deletion, a blank hole appears to be the size of the deletion before my new data is written.
This is what I've tried...
BufferedWriter bw;
try {
bw = new BufferedWriter(new FileWriter(path));
bw.write("");
bw.close();
}
catch (IOException e) {
e.printStackTrace();
}
And I've also tried this...
File f = new File(file);
FileWriter fw;
try {
fw = new FileWriter(f,false);
fw.write("");
}
catch (IOException e) {
e.printStackTrace();
}
Can someone please help me with a solution to this problem.
FileWriter (path, false)
The false will tell the writer to truncate the file instead of appending to it.
Try calling flush() before calling close().
FileWriter writer = null;
try {
writer = ... // initialize a writer
writer.write("");
writer.flush(); // flush the stream
} catch (IOException e) {
// do something with exception
} finally {
if (writer != null) {
writer.close();
}
}
It might be because you are not closing the FileWriter, fw.close(); also you dont need to "delete" the old data, just start writing and it will overwrite the old data. So make sure you are closing everywhere.
This works for me:
File f=new File(file);
FileWriter fw;
try {
fw = new FileWriter(f);
fw.write("");
fw.close();
} catch (IOException e) {
e.printStackTrace();
}

Categories