开发者

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"; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜