Results 1 to 9 of 9
  1. #1
    Torchi12 is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default 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;
        }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default 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.

  3. #3
    Torchi12 is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Try-catch-(finally?) on reading/writing to files

    Anyone..?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Try-catch-(finally?) on reading/writing to files

    Quote Originally Posted by Torchi12 View Post
    Anyone..?
    Apparently my first reply is invisible.

  5. #5
    Torchi12 is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Try-catch-(finally?) on reading/writing to files

    So, something like this?

    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();
    I have another questions.

    1. If I write:
    Java Code:
    PrintWriter pw = null;
    
    try
    {
    pw = blablabla...;
    }
    catch(blabla)
    {
    blabla
    }
    Do I need a finally to close pw?
    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.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default 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......

  7. #7
    Torchi12 is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Try-catch-(finally?) on reading/writing to files

    The printwriter doesnt throw an exception, but the filewriter does

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default 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.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Try-catch-(finally?) on reading/writing to files

    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();
    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).

    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

  1. Trouble with Try Catch blocks and file reading.
    By theBurgh22 in forum New To Java
    Replies: 2
    Last Post: 11-30-2010, 01:11 AM
  2. Reading / Writing files
    By Learning Java in forum New To Java
    Replies: 6
    Last Post: 08-08-2010, 09:21 PM
  3. Reading and Writing Text Files
    By kandt in forum New To Java
    Replies: 1
    Last Post: 11-12-2008, 03:15 AM
  4. Reading/Writing files through Applet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-15-2007, 08:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •