reading a file from current directory in java
I have a java project where I am reading a file. As the file is in the current directory I am doing this:
开发者_开发技巧String dataset = "./myFile.dat";
But I am getting: java.io.FileNotFoundException
saying It can not find the file.
How to fix this? When I give entire path it works...
String dataset = "C:\\eclipse\\workspace\\p1\\src\\myFile.dat";
If myFile.dat
is an application resource, it should be included in a Jar that is on the run-time class-path of the application. Then an URL to the resource can be formed using..
URL urlToData = this.getClass().getResource("path/in/jar/to/myFile.dat");
Don't rely on the user.dir
property. Depending on how the app. is started, it might point somewhere very different to the directory of the application or data.
Try this:
String dataset = System.getProperty("user.dir") + "/myFile.dat";
精彩评论