开发者

How to scan a file in a different directory in java?

How do you scan a file with java that isn't in the directory the java file is in?

For example: The java fil开发者_StackOverflow中文版e is located at "C:\Files\JavaFiles\test.java" However, the file I want to scan is located at "C:\Data\DataPacket99\data.txt"

Note: I've already tried putting another java file in the "C:\Data" directory and using the test.java file as a class, but it doesn't work. It still tries to scan from the "C:\Files\JavaFiles" Directory.


By using an absolute path instead of a relative.

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

Then you can write code that accesses that file object, using a InputStream or similar.


You need to use absolute paths in java.io stuff. Thus not new File("data.txt"), but new File("C:/Data/DataPacket99/data.txt"). Otherwise it will be relative to the current working directory which may not per-se be the same in all environments or the one you'd expect.


You should be using an absolute path instead of a relative path.

You could use File file = new File("C:/Data/DataPacket99/data.txt"); but it might make your life easier in the future to use a file chooser dialog if at any point the user will have to enter a file path.


I would try this:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜