Results 1 to 8 of 8
Thread: output to file? (tutoring)
- 10-29-2012, 04:46 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
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!)
- 10-29-2012, 05:21 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 10-30-2012, 03:12 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
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();
- 10-30-2012, 03:49 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 10-30-2012, 05:14 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
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.
- 10-30-2012, 05:26 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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.
- 10-30-2012, 06:19 PM #7
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(); }
- 10-31-2012, 04:08 PM #8
Member
- Join Date
- Oct 2012
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
Agent Based Tutoring System
By Amedeo_Sookaloo in forum Advanced JavaReplies: 3Last Post: 01-07-2012, 05:27 PM -
Read CSV File, sort records, and output a new csv file
By jrnowlan in forum New To JavaReplies: 1Last Post: 08-05-2011, 09:21 PM -
how to get the resultset output into an output file
By renu in forum New To JavaReplies: 0Last Post: 09-30-2010, 08:16 PM -
how to change the layout of an input file and write to an output file
By renu in forum New To JavaReplies: 8Last Post: 05-12-2010, 07:19 PM -
Search a word(taken from one file) in another file and give the line as the output
By SwapnaNaidu in forum New To JavaReplies: 7Last Post: 11-19-2008, 02:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks