简体   繁体   English

Printwriter目的地问题

[英]Printwriter destination issue

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. 抛出FileNotFoundException。 How do I get around this? 我该如何解决这个问题? Thank you! 谢谢!

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. 即使您只是从Eclipse运行它,在项目文件夹中写入也不是最好的想法。 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). 不仅可能意外覆盖您的源代码,但您的代码将不会非常便携,如果您稍后决定将所有内容打包在jar文件中,您将发现您将无法找到这些目录更多(因为他们都打包了)。

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 

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. 您可以选择使用项目中文件夹的完整路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM