Java 印刷机目的地问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22233631/
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
Printwriter destination issue
提问by MMP
I am successfully writing strings to a text file with the PrintWriter, and by default the output text file is written to the directory of the Eclipse project I'm working on.
我使用 PrintWriter 成功地将字符串写入文本文件,默认情况下,输出文本文件被写入我正在处理的 Eclipse 项目的目录中。
But my Eclipse project has a specific folder called Resources that I want the text file to be written to:
但是我的 Eclipse 项目有一个名为 Resources 的特定文件夹,我希望将文本文件写入其中:
My code is:
我的代码是:
protected void saveCommandsToFile() throws FileNotFoundException, IOException {
PrintWriter out = new PrintWriter("commands.txt");
int listsize = list.getModel().getSize();
for (int i=0; i<listsize; i++){
Object item = list.getModel().getElementAt(i);
String command = (String)item;
System.out.println(command); //use console output for comparison
out.println(command);
}
out.close();
}
If I change the line to:
如果我将行更改为:
PrintWriter out = new PrintWriter("/Resources/commands.txt");
The FileNotFoundException is being thrown. How do I get around this? Thank you!
正在抛出 FileNotFoundException。我该如何解决这个问题?谢谢!
采纳答案by biziclop
A PrintWriter
created this way will expect a path, which can be either relative or absolute.
以PrintWriter
这种方式创建的将需要一个路径,该路径可以是相对的或绝对的。
Relative paths are relative to the working directory.
相对路径是相对于工作目录的。
Absolute paths on the other hand need to contain the full path from the root directory (or directories, as it happens), so on a Windows box it'll be something like c:/foo/bar.txt
, on Unix-type systems /home/nobody/foo/bar.txt
.
另一方面,绝对路径需要包含来自根目录(或目录,因为它发生)的完整路径,因此在 Windows 机器上,它类似于c:/foo/bar.txt
Unix 类型系统上的/home/nobody/foo/bar.txt
。
The exact rules on absolute and relative paths can be found here.
可以在此处找到关于绝对路径和相对路径的确切规则。
A note though on the use of relative paths.Be careful when you're relying on them because you have no way of knowing what your working directory is going to be: when you run your application from Eclipse, it will default to your project directory but if you package it up and run from command line, it will be somewhere else.
关于使用相对路径的注意事项。当您依赖它们时要小心,因为您无法知道您的工作目录将是什么:当您从 Eclipse 运行您的应用程序时,它将默认为您的项目目录,但如果您将其打包并从命令运行行,它将在其他地方。
And even if you're only running it from Eclipse, writing in your project folder isn't the best of ideas. Not only is it possible to accidentally overwrite your source code, but your code won't be very portable, if later on you decide to package everything up in a jar file, you'll find you won't be able to find those directories any more (because they're all packaged up).
即使您只是从 Eclipse 运行它,在您的项目文件夹中写入也不是最好的主意。不仅可能会意外覆盖您的源代码,而且您的代码也不是很容易移植,如果稍后您决定将所有内容打包在一个 jar 文件中,您会发现您将无法找到那些目录没有更多(因为它们都打包了)。
回答by Richard Miskin
The path you are specifying is an absolute path. If you want it to be relative to where you're running the java program try using "./Resources/commands.txt"
.
您指定的路径是绝对路径。如果您希望它与运行 java 程序的位置相关,请尝试使用"./Resources/commands.txt"
.
Alternative you could use the full path to the folder within your project.
或者,您可以使用项目中文件夹的完整路径。
回答by Braj
Try below code:
试试下面的代码:
protected static void saveCommandsToFile() throws FileNotFoundException, IOException {
File file = new File("resources/commands.txt");
System.out.println("Absolute path:" + file.getAbsolutePath());
if (!file.exists()) {
if (file.createNewFile()) {
PrintWriter out = new PrintWriter(file);
out.println("hi");
out.close();
}
}
}
Here is the project folder structure:
这是项目文件夹结构:
Project
|
|___src
|
|___resources
|
|___commands.txt