File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes NX:window XP - java.io.FileNotFoundException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "NX:window XP - java.io.FileNotFoundException" Watch "NX:window XP - java.io.FileNotFoundException" New topic
Author

NX:window XP - java.io.FileNotFoundException

Zhi Gang
Ranch Hand

Joined: May 03, 2003
Posts: 30
hi there, i have been pulling out my hair + jumping up and down for this problem, if any one have ideal pleae let me know
operating system: window XP (it works fine if i use window NT/2000)
file name : record.db
this code is suppose to get the file and read it :
---------------------------------------------------
String fname = "record.db"; // or String fname = ".\\record.db";
FileInputStream fis = new FileInputStream(fname);
DataInputStream dis = new DataInputStream(fis);
and it throw
java.io.FileNotFoundException: record_db\record.db(The system find the specified)
-----------------------------------------------
BUT if i use the below line it works fine
String fname = "c:\javaProject\classes\record.db";
and i need to make this dynamic so that i can jar it
please let me know
p/s: i even tried the example java developer exam Book isbn = 1590590309 , it works for win 2000 or NT but not for XP
frank sun
Ranch Hand

Joined: Apr 11, 2003
Posts: 102
Did you try to use the Properties to store your file name? it will save your file name with the native approach.
FYR.
Frank
Zhi Gang
Ranch Hand

Joined: May 03, 2003
Posts: 30
do u mean use
Properties recName = new Properties();
recName.setPropery(path,"record.db");
FileInputStream fis = new FileInputStream(path);
??
it deosn't seen to work .. can u fix this?
============================================
Properties recName = new Properties();
recName.setPropery(path,"record.db");
String recPath = recName.getProperty(path);
FileInputStream fis=new FileInputStream(recPath);
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Actually your issue lies in the java command and the directory that you are starting your application in compared to the directory where the record.db file actually is.
In your code you are assuming that these two directories are the same, and in actuality they are different.
Here's the proof. Your error shows
"record_db\record.db"
so the application is starting up in the record_db directory when you run the java commandline.
But the record.db file is actually in your
c:\javaProject\classes\record.db
directory.
So of course it won't find the file.
Now, unfortunately you do not want to hard code drive and paths. The reason is that your code will not be portable and inflexible. If you try to run your code on a UNIX box, you will faill because UNIX has no concept of drive letters.
There are many ways to approach this problem.
1. Yes you can use a properties file to store the path, but if you move it to UNIX, you have to change the properties file to reflect that.
2. You can use some of Java's built in Java "Constants" like
System.getProperty("user.dir");
This will return the directory that you are running the Java app from, in OS format that it is running on.
There is another property for OS path separator, which I can't remember off the top of my head.
With both of these you can construct a path to the file that is a "Relative" path.
In this solution you never have to change any file or code to port to a different machine or OS.
Just have the .db file in the root directory from where you are running the app, or you can make a subdirectory in that path to store the .db file, and you can even have it in a jar file and use one of the getResource type methods to read and write to the file in the jar.
However, in this solution you cannot change the location of where the .db file resides. Where in the properties solution you can.
Personally I used the 2nd solution, and most likely would chose the 2nd solution when it comes to an integral file of an application.
There are sometimes when the properties solution is the better solution. As always it is an "It depends" decision. And in the assignment I go for solution 2.
Hope that helps.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17260
    
    6

Oh, by the way, do you have any video of you pulling out your hair and jumping up and down?
We have a special video library here at JavaRanch of such antics that we Bartenders like to laugh at. JK.
Mark
Zhi Gang
Ranch Hand

Joined: May 03, 2003
Posts: 30
waooohh .. the 1st time i got the attention from the bartender .. cool cool ..
i had tried on this.. and it works! yabadabadooo..
String whereAMi = System.getProperty("user.dir");
and assignement instruction:-
-----------------------------------
The JAR file must have the following layout and contents in its root:
1.The executable JAR containing the programs.
2.The original, unchanged database file that was supplied to you.
i guess i am going to 2 choice and the Application will find it the .db in whereAMi/record.db
sound cool?
anyway.. thanks for saving mine other half of hair
... i shall remember to video recorded, if i jumping up and down and send it to you guys..
thanks, it really mean alot
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NX:window XP - java.io.FileNotFoundException