Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Exclamation output to file? (tutoring)

    i have been trying to print a programs output to a file, but using all examples in the book I either get a compile error or a run with no file created. I've been searching my book over time and time again. can someone show my an example of how it should look with the printwriter/filewriter classes? (it sucks tutoring someone when your own class didn't cover that section yet!)

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

    Default Re: output to file? (tutoring)

    You'll need to post your attempts here along with the error/exceptions you get, highlighting the line(s) they occur on.
    Remember to use [code] tags [/code] around your code, otherwise the formatting will vanish.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: output to file? (tutoring)

    file name is to be file test. but it's giving my an error with the printwriter class and won't finish compiling.
    the compiler is giving me this error"

    BarChartProgram.java:82: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
    PrintWriter outputFile = new PrintWriter(fwriter);


    Java Code:
    	String fwriter = JOptionPane.showInputDialog("enter file name. file test");
    	PrintWriter outputFile = new PrintWriter(fwriter);
    	outputFile.println("Testing...");
    	outputFile.println("Testing..");
    	outputFile.println("Testing.");
    	outputFile.close();

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

    Default Re: output to file? (tutoring)

    Pretty much anything involving IO to files can result in an exception (usually an IOException).
    FileNotFoundException is s subclass of IOException and is thrown by the constructor of your Writer.
    You need to handle it.

    If you don't know about Exceptions then here's the Oracle tutorial on them.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: output to file? (tutoring)

    That still isn't making any sense to me and trying what they have it's not working properly, if I change anything with help from my understanding of the oracle page, I get 4-6 errors.

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

    Default Re: output to file? (tutoring)

    Then you will need to show us the modified code (that tries to handle the FileNotFOundException) and copy the errors you are getting to here, highlighting the line(s) they occur on.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: output to file? (tutoring)

    I agree with Tolls. Are you saying you don't understand how to catch an exception? Are you doing something like this:

    Java Code:
    try{
        PrintWriter outputFile = new PrintWriter(fwriter);
        outputFile.println("Testing...");
        outputFile.println("Testing..");
        outputFile.println("Testing.");
        outputFile.close();
    } catch(IOException e){
        e.printStackTrace();
    }
    ??

  8. #8
    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Default Re: output to file? (tutoring)

    yea i AM having issues with the try and catch, throw commands, the oracle website is rather confusing for me on the topic. thanks for the example, I'll give it a try and let you know.

Similar Threads

  1. Agent Based Tutoring System
    By Amedeo_Sookaloo in forum Advanced Java
    Replies: 3
    Last Post: 01-07-2012, 05:27 PM
  2. Replies: 1
    Last Post: 08-05-2011, 09:21 PM
  3. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  4. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  5. Replies: 7
    Last Post: 11-19-2008, 02:09 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
  •