Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX:window XP - java.io.FileNotFoundException

 
Zhi Gang
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic