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!
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
Code:
outputStream = new PrintWriter(fileName);
It will tell you that the next line that access the outputStream variable might have not been initialized.
Re: Filewriter Triggered by JButton Error: FileNotFoundException. Help!
Re: Filewriter Triggered by JButton Error: FileNotFoundException. Help!
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.