FileNotFoundException
Hello,
I'm new to Java and got a problem. Here's the code:
import java.io.*;
publicclass Machine
{
publicstaticvoid main(String[] args)
{
File panama_file =new File("panama.txt");
FileReader reader =new FileReader(panama_file);
reader.close();
}
}
I get FileNotFoundException exception when running it. That would seem like a no-brainer exception to figure out, BUT panama.txt IS in the same directory as Machine.java. What could be the problem?
Btw, I am using eclipse and Win XP SP2.
[973 byte] By [
Reea] at [2007-10-2 7:32:25]

Depending on assumed directory paths is a bad idea. Figure out a way you can always supply the correct path.
I have also tried putting the file to another dir and passing full path ("C:/Temp/panama.txt"). No luck, the same exception.
Reea at 2007-7-16 21:12:16 >

Are you sure that you don't have a spelling mistake in the filename?
kajbja at 2007-7-16 21:12:16 >

Yeah, there's no mistake. This is exactly what I get in console when I run it:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Unhandled exception type FileNotFoundException
Unhandled exception type IOException
at Machine.main(Machine.java:8)
Reea at 2007-7-16 21:12:16 >

Your code shouldn't even compile. There are operations there that throw checked exceptions and you are not handling those exceptions. What JDK / Java compiler / IDE are you using? Get the real JDK from java.sun.com.
> Yeah, there's no mistake. This is exactly what I get
> in console when I run it:
> > Exception in thread "main" java.lang.Error:
> Unresolved compilation problems:
> Unhandled exception type FileNotFoundException
> Unhandled exception type IOException
>
> at Machine.main(Machine.java:8)
>
That means that you have a compilation exception. The problem is not that your file can't be found.
Kaj
kajbja at 2007-7-16 21:12:16 >

All right, got your point, will fix it.
Reea at 2007-7-16 21:12:16 >

import java.io.*;
public class Machine
{
public static void main(String[] args)
{
try
{
FileReader reader = new FileReader("C:/Temp/panama.txt");
try
{
reader.close();
} catch (IOException e)
{
System.out.println("Unable to close file!");
}
} catch (FileNotFoundException e)
{
System.out.println("File not found!");
}
}
}
This one works fine now. However, I'd like to ask - do I actually need to close the file, or is it ok to leave this (until the program ends)? Also what is the point in having an exception for closing the file?
Reea at 2007-7-16 21:12:16 >

You should close a file when you no longer need it to read / write.
kajbja at 2007-7-16 21:12:16 >

Yeah, but is it just one of the 'good practices', or does it really matter in some way and may cause some problems if not done?
Reea at 2007-7-16 21:12:16 >

> Yeah, but is it just one of the 'good practices', or
> does it really matter in some way and may cause some
> problems if not done?
It matters. Unless you are certain the process running your code will terminate at a certain point - which you can't really be certain of - even if it is the 'public static void main(String[] args)' method which you code that in. This method may be invoked by another 'app' in the same VM as that other app, and then you've leaked a file handle.
It's especially important to close them if you're writing to the file - otherwise you may lose the last chunk of data written to it, as it is just in a buffer and hasn't been committed to disk yet.
HiI think this is just a good practice. The moment you find out why, you might have accidently overwritten that file... Also, if you want to access the file outside JVM (i.e to read changes you are doing in the program), it certainly is good for it to be closed.Fluid
Fluida at 2007-7-16 21:12:16 >

yes it is a good measure to always close the file!!!