aspose file tools*
The moose likes I/O and Streams and the fly likes FileIO: java.io.FileNotFoundException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "FileIO: java.io.FileNotFoundException" Watch "FileIO: java.io.FileNotFoundException" New topic
Author

FileIO: java.io.FileNotFoundException

January Montague
Greenhorn

Joined: Jan 27, 2005
Posts: 5
Hello.

I had my code working on my old 32 bit Windows XP machine.
I had to transfer all of my files to a new laptop, a 64 bit Windows 7 machine.

Somehow in that transfer my Java program quit compiling.
I'm not sure if it's some weird fluke of the transfer to the new machine or some thing I did or didn't do in setting up my environment properly.

Any help or advice would be greatly appreciated.

Basically I"m getting a FileIO: java.io.FileNotFoundException Error.
I have already checked that my file is still in the same folder in which my .class and .java files are.

The odd thing is I perform a getCanonicalPath() operation and that tells me my file is actually 3 levels up, which doesn't make any sense.
I'm not sure why the program is looking there. I haven't changed any code to tell it to look anywhere else for the file.

Please let me know what other information I could post that would be helpful.

Thanks
January
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19542
    
  16

Windows Vista and Windows 7 have a different structure inside the user's personal folder. Some directories that you know from Windows XP and before are now actually symbolic links to their matching directories in Windows Vista / 7, but java.io.File can't handle those symbolic links well. java.nio.file.Path, added in Java 7, should have no problems though.
Apart from that, Windows Vista and Windows 7 have added a better security mechanism by default. This blocks quite a lot of programs, including Java programs, from writing to anywhere inside the Program Files and Windows directories.

Can you show us the absolute and canonical paths of the File you're trying to use?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Rob Spoor wrote:Windows Vista and Windows 7 have added a better security mechanism by default.

Hehe, whether what Vista did was better is debatable, methinks. It was certainly more annoying to the average user :-)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19542
    
  16

Annoying, yes; I've turned it off completely. But it definitely is better security.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18124
    
    8

January Montague wrote:Basically I"m getting a FileIO: java.io.FileNotFoundException Error.
I have already checked that my file is still in the same folder in which my .class and .java files are.


Then that suggests that the current working directory is different on the new machine, and you are using a relative path to identify the file.

It seems like a lot of beginners miss the class where "current working directory" is taught and just assume that relative paths are relative to their code. That isn't the case.
January Montague
Greenhorn

Joined: Jan 27, 2005
Posts: 5
Hello Rob-

Thanks for the quick reply.
First off I should let you know I am using Java 1.4.2.
I know Java has been updated extensively since then but due to program constraints and requirements we have to remain with Java 1.4.2.
Also I have 4 classes all packaged as com.company.NAME, and I also have the files in a corresponding file structure C:\projects\NAME\maint\com\company\NAME
My maint_cfg.txt file is located in the directory structure where my .java and .class files are. In the file structure listed above.
For some reason when I run the canonical and absolute path commands Java seems to be looking for the .txt file a few levels up.
Again this is new to this computer. The program worked fine on the old Windows XP laptop before I transferred to Windows 7.

The absolute and canonical paths of the File are below:
Canonical: getCanonicalPath() = C:\projects\NAME\maint\maint_cfg.txt
Absolute: getAbsolutePath() = C:\projects\NAME\maint\maint_cfg.txt



The small bit of code I'm using is here:

try {
in = new BufferedReader(new FileReader("maint_cfg.txt"));

while (in.ready())
{
fileLine = in.readLine();

............etc.........


Thanks for your help and let me know if I can post anything else.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

January Montague wrote:Hello Rob-

Thanks for the quick reply.
First off I should let you know I am using Java 1.4.2.
I know Java has been updated extensively since then but due to program constraints and requirements we have to remain with Java 1.4.2.
Also I have 4 classes all packaged as com.company.NAME, and I also have the files in a corresponding file structure C:\projects\NAME\maint\com\company\NAME
My maint_cfg.txt file is located in the directory structure where my .java and .class files are.


None of that matters. You're trying to use a relative path. That will be relative to the System Property "user.dir" (http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#getProperties()). That property is set based on the "current working directory" at the time the JVM starts up. If you're starting it from a command line, it will be the directory you are "in" in your command prompt when you start it. That directory has no relation at all to where any .java or .class file is, unless certain environments, say an IDE, coincidentally happen to set the current directory to something related to those paths before launching the VM.

Your choices are:

1) Use a full path.

2) Control the startup working directory of the JVM.

3) If you want to retrieve files relative to your classpath, use ClassLoader (or is it Class? Or Thread?).getResource() or getResourceAsStream().
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19542
    
  16

And next time please UseCodeTags. That saves you the trouble of adding your own colour coding - that's then done automatically, and you also to keep your indentation.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: FileIO: java.io.FileNotFoundException
 
Similar Threads
File transfer
Unable to find virtual machine
File Path in Mac and Windows
can I ask a big favor?
Springapp Tutorial FileNotFoundException at Step 1.11, running ant deploy reload