Java 写入文件,输出文件在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3515745/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:26:54  来源:igfitidea点击:

Writing to a file, where is the output file?

javaeclipsefile-io

提问by KJW

        FileWriter outFile = null;
        try {
            outFile = new FileWriter("member.txt");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
out.println("test");

Running that command, where is the member.txt ? I am using windows vista. UAC enabled so when I run it, I don't think it's writing to the txt file. txt file is created however, but it's empty.

运行该命令, member.txt 在哪里?我正在使用Windows Vista。启用了 UAC,所以当我运行它时,我认为它不会写入 txt 文件。txt 文件已创建,但它是空的。

采纳答案by BalusC

Relative paths in Java IO are relative to current working directory. In Eclipse, that's usually the project root. You're also writing to outinstead of outFile. Here's a minor rewrite:

Java IO 中的相对路径是相对于当前工作目录的。在 Eclipse 中,这通常是项目根目录。您也在写入out而不是outFile. 这是一个小的重写:

    File file = new File("member.txt");
    FileWriter writer = null;
    try {
        writer = new FileWriter(file);
        writer.write("test");
    } catch (IOException e) {
        e.printStackTrace(); // I'd rather declare method with throws IOException and omit this catch.
    } finally {
        if (writer != null) try { writer.close(); } catch (IOException ignore) {}
    }
    System.out.printf("File is located at %s%n", file.getAbsolutePath());

Closing is mandatory since it flushes the written data into the file and releases the file lock.

关闭是强制性的,因为它将写入的数据刷新到文件中并释放文件锁。

Needless to say that it's a poor practice to use relative paths in Java IO. If you can, rather make use of the classpath. ClassLoader#getResource(), getResourceAsStream()and so on.

不用说,在 Java IO 中使用相对路径是一种糟糕的做法。如果可以,最好使用类路径。ClassLoader#getResource()getResourceAsStream()等等。

回答by erickson

If the file is successfully created (no exception is raised), it is in the current working directory.

如果文件创建成功(没有引发异常),则它位于当前工作目录中。

回答by Aaron Hathaway

It depends on the IDE you're using also. It will usually go into the same directory that the file.java is located at. I think programs like Eclipse and Netbeans may toss it in a different directory.

这也取决于您使用的 IDE。它通常会进入 file.java 所在的同一目录。我认为像 Eclipse 和 Netbeans 这样的程序可能会将它扔到不同的目录中。

回答by Mark Peters

If running from Eclipse, the current working directory will be your project's base directory (view your project properties to find that location on disk). You should be able to see the file in the Project Explorer by refreshing the project (click on the project and hit F5).

如果从 Eclipse 运行,当前工作目录将是您项目的基本目录(查看您的项目属性以在磁盘上找到该位置)。通过刷新项目(单击项目并点击F5),您应该能够在项目资源管理器中看到该文件。

You can specify an alternative working directory from the Run Configurations dialog under the Arguments tab.

您可以从“参数”选项卡下的“运行配置”对话框中指定替代工作目录。

回答by Brent Writes Code

For the Java class you're executing, right click on the file and go to "Run As -> Run Configurations..."

对于您正在执行的 Java 类,右键单击该文件并转到“运行方式 -> 运行配置...”

In this screen, go to the "Arguments" tab. At the bottom of the screen, look for the "Working directory" setting. This is the directory that your Java class will run from.

在此屏幕中,转到“参数”选项卡。在屏幕底部,查找“工作目录”设置。这是您的 Java 类将运行的目录。

In your example, you're creating "member.txt" in the current directory, so it will show up in whatever location your "Working directory" is set to.

在您的示例中,您正在当前目录中创建“member.txt”,因此它将显示在“工作目录”设置的任何位置。