Java 如何将文本文件作为参数传递?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3309899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass a text file as a argument?
提问by Victor
Im trying to write a program to read a text file through args but when i run it, it always says the file can't be found even though i placed it inside the same folder as the main.java that im running. Does anyone know the solution to my problem or a better way of reading a text file?
我正在尝试编写一个程序来通过 args 读取文本文件,但是当我运行它时,它总是说找不到该文件,即使我将它与正在运行的 main.java 放在同一文件夹中。有谁知道我的问题的解决方案或阅读文本文件的更好方法?
采纳答案by BalusC
Do not use relative paths in java.io.File
.
不要在java.io.File
.
It will become relative to the current working directory which is dependent on the way how you run the application which in turn is not controllable from inside your application. It will only lead to portability trouble. If you run it from inside Eclipse, the path will be relative to /path/to/eclipse/workspace/projectname
. If you run it from inside command console, it will be relative to currentlyopened folder (even though when you run the code by absolute path!). If you run it by doubleclicking the JAR, it will be relative to the root folder of the JAR. If you run it in a webserver, it will be relative to the /path/to/webserver/binaries
. Etcetera.
它将变得相对于当前工作目录,这取决于您运行应用程序的方式,而这又是无法从您的应用程序内部控制的。它只会导致便携性问题。如果从 Eclipse 内部运行它,路径将相对于/path/to/eclipse/workspace/projectname
. 如果您从命令控制台内部运行它,它将相对于当前打开的文件夹(即使您通过绝对路径运行代码!)。如果通过双击 JAR 运行它,它将相对于 JAR 的根文件夹。如果您在网络服务器中运行它,它将相对于/path/to/webserver/binaries
. 等等。
Always use absolute paths in java.io.File
, no excuses.
始终在 中使用绝对路径java.io.File
,没有任何借口。
For best portability and less headache with absolute paths, just place the file in a path covered by the runtime classpath (or add its path to the runtime classpath). This way you can get the file by Class#getResource()
or its content by Class#getResourceAsStream()
. If it's in the same folder (package) as your current class, then it's already in the classpath. To access it, just do:
为了获得最佳可移植性并减少绝对路径的麻烦,只需将文件放在运行时类路径覆盖的路径中(或将其路径添加到运行时类路径中)。通过这种方式,您可以Class#getResource()
通过Class#getResourceAsStream()
. 如果它与您当前的类在同一个文件夹(包)中,那么它已经在类路径中。要访问它,只需执行以下操作:
public MyClass() {
URL url = getClass().getResource("filename.txt");
File file = new File(url.getPath());
InputStream input = new FileInputStream(file);
// ...
}
or
或者
public MyClass() {
InputStream input = getClass().getResourceAsStream("filename.txt");
// ...
}
回答by chimeracoder
Try giving an absolute path to the filename.
尝试给出文件名的绝对路径。
Also, post the code so that we can see what exactly you're trying.
另外,发布代码,以便我们可以看到您到底在尝试什么。
回答by perimosocordiae
If you're using Eclipse (or a similar IDE), the problem arises from the fact that your program is run from a few directories above where the actual source is located. Try moving your file up a level or two in the project tree.
如果您使用的是 Eclipse(或类似的 IDE),则问题在于您的程序是从实际源所在的几个目录运行的。尝试将您的文件在项目树中上移一两层。
Check out this questionfor more detail.
查看此问题了解更多详情。
回答by Govan
If you put the file and the class working with it under same package can you use this:
如果你把文件和使用它的类放在同一个包下,你可以使用这个:
Class A {
void readFile (String fileName) {
Url tmp = A.class.getResource (fileName);
// Or Url tmp = this.getClass().getResource (fileName);
File tmpFile = File (tmp);
if (tmpFile.exists())
System.out.print("I found the file.")
}
}
It will help if you read about classloaders.
如果您阅读有关类加载器的信息,将会有所帮助。
回答by Omry Yadan
When you are opening a file with a relative file name in Java (and in general) it opens it relative to the working directory.
当您在 Java(以及一般情况下)中打开具有相对文件名的文件时,它会相对于工作目录打开它。
you can find the current working directory of your process using
您可以使用找到进程的当前工作目录
String workindDir = new File(".").getAbsoultePath()
Make sure you are running your program from the correct directory (or change the file name so that it will be relative to where you are running it from).
确保从正确的目录运行程序(或更改文件名,使其与运行程序的位置相关)。
回答by Truong Ha
The simplest solution is to create a new file, then see where the output file is. That is the correct place to put your input file into.
最简单的解决方法是新建一个文件,然后查看输出文件在哪里。这是将输入文件放入的正确位置。