简体   繁体   中英

How to specify path to file in Java?

how to add path to File Scanner in Java? I use Ubuntu and Intellij. I think I do everythink ok, but it still doesn't work:(

Code:

 File file = new File("/abc/file.txt");

Error:

java.io.FileNotFoundException: /abc/file.txt (No such file or directory)

情报

Your abc folder is under src and you're using absolute pathing with the leading / which states it at the root of your file system.

Move the folder to the root folder of the project and then reference is as

File file = new File("abc/file.txt");

Assuming in linux you have this file in correct path: eg. /path/to/file/filename.txt

You can try this:

For windows:

File file = new File("\\path\\to\\file\\filename.txt");

For Linux:

File file = new File("/path/to/file/filename.txt");

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