Results 1 to 9 of 9
- 11-28-2011, 11:20 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
Try-catch-(finally?) on reading/writing to files
When reading or writing to files, what should you have in a try-catch?
And should you have a finally?
Should I have everything in the try?
Like.. maybe create a File variable to check if a file with that name exists etc and throw errors if they do.
And then open the file and write to it?
And have the printWriterVariable.close() in the finally?
or something like this..
Java Code:PrintWriter pw = null; try { pw = new PrintWriter( new BufferedWriter( new FileWriter(filNamn, true) ) ); } catch(IOException error) { System.out.println("\Something went wrong, e.message:"+error.getMessage()); return; }
- 11-28-2011, 11:39 PM #2
Re: Try-catch-(finally?) on reading/writing to files
There really isn't any rule as to how you write a try statement. If you are lazy you can just make all your methods throw the exception and let the JVM handle it. Or you can restrict the scope of the try statement by only placing the code that can cause the exception inside it. Or anything in between. However, you should try and restrict the scope as much as possible.
- 11-28-2011, 11:41 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
Re: Try-catch-(finally?) on reading/writing to files
Anyone..?
- 11-29-2011, 12:06 AM #4
- 11-29-2011, 12:19 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
Re: Try-catch-(finally?) on reading/writing to files
So, something like this?
I have another questions.Java Code:File aFile = new File("Hey.txt"); if( aFile.exists() ) System.out.println("Exists"); //I dont know, something like - file exists, want to write over it? //if no, end function PrintWriter pw = null; try { pw = new PrintWriter(new BufferedWriter(new FileWriter("Hey.txt") ) ); } catch(IOException error) { System.out.println("Error: "+error.getMessage() ); } pw.println("some stuff"); pw.println("Some more stuff"); pw.close();
1. If I write:
Do I need a finally to close pw?Java Code:PrintWriter pw = null; try { pw = blablabla...; } catch(blabla) { blabla }
If yes - I always have to write everything inside the try?
But if something goes wrong in that try, then it was never open right? a close is not needed?Last edited by Torchi12; 11-29-2011 at 12:21 AM.
- 11-29-2011, 12:27 AM #6
Re: Try-catch-(finally?) on reading/writing to files
Since none of the methods of PrintWriter throw an exception your code does not have to go in the try statement. Or you could place it inside the try statement and have the call to close in the finally or not. Maybe even use an if statement to see if the writer needs to be closed or not. Or......
- 11-29-2011, 12:30 AM #7
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
Re: Try-catch-(finally?) on reading/writing to files
The printwriter doesnt throw an exception, but the filewriter does
- 11-29-2011, 12:46 AM #8
Re: Try-catch-(finally?) on reading/writing to files
The short and curly is code that may throw an exception MUST go inside a try statement*. Code that doesn't throw an exception may or may not go inside the try statement. You decide.
* unless the method throws the exception.
- 11-29-2011, 01:42 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Try-catch-(finally?) on reading/writing to files
If the above is a FileWriter (rather than PrintWriter) then the above is broken...if the creation of the writer fails then you will get a NullPointerException when you try and use it. Throw that exception (or a more generic one with the IO exception wrapped in it).Java Code:File aFile = new File("Hey.txt"); if( aFile.exists() ) System.out.println("Exists"); //I dont know, something like - file exists, want to write over it? //if no, end function PrintWriter pw = null; try { pw = new PrintWriter(new BufferedWriter(new FileWriter("Hey.txt") ) ); } catch(IOException error) { System.out.println("Error: "+error.getMessage() ); } pw.println("some stuff"); pw.println("Some more stuff"); pw.close();
As for finally, in general you would have the processing code inside the try block since attempting to use the writer after a FileNotFound (for example) makes no sense...and therefore you close the writer in the finally block to ensure it is closed.
ETA: Oh, just noticed it was a FileWriter wrapped in a PrintWriter...still, the rest stands.
Similar Threads
-
Trouble with Try Catch blocks and file reading.
By theBurgh22 in forum New To JavaReplies: 2Last Post: 11-30-2010, 01:11 AM -
Reading / Writing files
By Learning Java in forum New To JavaReplies: 6Last Post: 08-08-2010, 09:21 PM -
Reading/Writing files through Applet
By Java Tip in forum Java TipReplies: 1Last Post: 03-09-2009, 11:45 AM -
Reading and Writing Text Files
By kandt in forum New To JavaReplies: 1Last Post: 11-12-2008, 03:15 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks