How to scan a file in another directory in java?

How do you scan a file with java that is not in the directory where the java file is located?

For example: a java file is located in the folder "C: \ Files \ JavaFiles \ test.java". However, the file I want to check is located in the folder "C: \ Data \ DataPacket99 \ data.txt"

Note. I already tried putting another Java file in the "C: \ Data" directory and using the test.java file as a class, but it does not work. It is still trying to scan from the "C: \ Files \ JavaFiles" directory.

+3
source share
4 answers

Using an absolute path instead of a relative one.

File file = new File("C:\\Data\\DataPacket99\\data.txt");

, , InputStream .

+3

java.io. , new File("data.txt"), new File("C:/Data/DataPacket99/data.txt"). , , .

+3

.

File file = new File("C:/Data/DataPacket99/data.txt");, , .

+2

:

File file = new File("../../Data/DataPacket99/data.txt");
+1

All Articles