Results 1 to 5 of 5
- 01-17-2013, 03:48 AM #1
Member
- Join Date
- Jan 2013
- Posts
- 14
- Rep Power
- 0
Filewriter Triggered by JButton Error: FileNotFoundException. Help!
Ok, so i am making a program that writes a file when a button is pressed. Here is part of my code which contains an error which i cannot seem to solve.
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == generateitemfile)
{
String fileName = "out.java";
PrintWriter outputStream;
outputStream = new PrintWriter(fileName);
outputStream.println("testing");
outputStream.flush();
}
}
The line: outputStream = new PrintWriter(fileName);
Gives the error: Unhandled exception type FileNotFoundException
It suggests using try/catch, but that causes a error in the outstream lines.
Help!
- 01-17-2013, 03:57 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
Re: Filewriter Triggered by JButton Error: FileNotFoundException. Help!
You can try to put everything inside the try-catch block. Because when you only put the following line of code inside the try-catch block
Java Code:outputStream = new PrintWriter(fileName);
Website: Learn Java by Examples
- 01-17-2013, 04:07 AM #3
Member
- Join Date
- Jan 2013
- Posts
- 14
- Rep Power
- 0
Re: Filewriter Triggered by JButton Error: FileNotFoundException. Help!
Awesome, thanks!
- 01-17-2013, 04:52 AM #4
Re: Filewriter Triggered by JButton Error: FileNotFoundException. Help!
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 01-17-2013, 05:34 PM #5
Senior Member
- Join Date
- Dec 2012
- Posts
- 106
- Rep Power
- 0
Re: Filewriter Triggered by JButton Error: FileNotFoundException. Help!
Also, when you catch a FileNotFoundException, you can find the full path name of where Java is looking for the file.
File file = new File(fileName);
System.out.println(fileName.getAbsolutePath());
This will print the full path name in case your file is one place and Java is looking another, you can see where Java is looking.
Similar Threads
-
FileWriter error
By kthomp271828 in forum Advanced JavaReplies: 17Last Post: 11-29-2011, 07:45 PM -
can't add to JPanel after removeAll() is triggered by another swing component
By apchar in forum AWT / SwingReplies: 8Last Post: 07-05-2011, 11:12 AM -
Error in writing JButton Array of objects into File
By justbeller in forum AWT / SwingReplies: 3Last Post: 06-09-2011, 12:06 AM -
FileWriter error
By bczm8703 in forum JCreatorReplies: 0Last Post: 03-04-2011, 05:10 AM -
i need assistance with a string triggered loop please!
By Phobos0001 in forum New To JavaReplies: 9Last Post: 11-14-2007, 03:44 PM
Bookmarks