简体   繁体   中英

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.

尝试这个:

String dataset = System.getProperty("user.dir") + "/myFile.dat"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM