如何在java中扫描不同目录中的文件? [英] How to scan a file in a different directory in java?
问题描述
例如:java文件位于C :\Files\JavaFiles\test.java但是,我要扫描的文件位于C:\Data\DataPacket99\data.txt
注意:我已经尝试将另一个java文件放在C:\Data目录中,并使用test.java文件作为一个类,但它不起作用。它仍然尝试从C:\Files\JavaFiles目录扫描。
通过使用绝对路径的一个亲戚。
文件文件=新文件(C:\\\\DataPacket99\\\ \\data.txt);
然后,您可以使用InputStream或类似的方式编写访问该文件对象的代码。
How do you scan a file with java that isn't in the directory the java file is in?
For example: The java file 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.
这篇关于如何在java中扫描不同目录中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!