java java在哪里查找文件?

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

Where does java look for files?

javafile

提问by Anonymous181

I'm trying to read a file in java:

我正在尝试用 Java 读取文件:

Public class Test{

public static void main (String [] args) throws IOException {

BufferedReader f = new BufferedReader(new FileReader("test.in"));

//...

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("test.out")));

//...                                 

}

}

1) Where should the location of "test.in" be? (src? bin? ???)

1)“test.in”的位置应该在哪里?(源代码?斌????)

2) Where will "test.out" be located?

2)“test.out”将位于何处?

采纳答案by OmniOwl

Usually it's the exact location of where the Java file you are executing is. So if you use your Java file in C:\ and you look for a file without specifying a path, it will be in C:\ that it looks, and the output file will also be in C:\

通常它是您正在执行的 Java 文件所在的确切位置。因此,如果您在 C:\ 中使用 Java 文件,并且在未指定路径的情况下查找文件,则它看起来将在 C:\ 中,而输出文件也将在 C:\

If you are using something like Netbeans however, you just have to place the file in the netbeans project folder of your specific project (the root directory folder). Not in the source or bin folders of your project.

但是,如果您使用的是 Netbeans 之类的东西,则只需将该文件放在特定项目的 netbeans 项目文件夹中(根目录文件夹)。不在项目的源或 bin 文件夹中。

回答by MeBigFatGuy

A relative path'ed file is looked for in the directory specified by System.getProperty("user.dir")

在 System.getProperty("user.dir") 指定的目录中查找相对路径的文件

回答by Hakan Serce

The answer is the working folder, which means the folder in which you executed the "java ..." command. This is the project folder in eclipse projects for instance.

答案是工作文件夹,即您在其中执行“java ...”命令的文件夹。例如,这是 eclipse 项目中的项目文件夹。

回答by paulsm4

I've probably spent way too long on this question, but:

我可能在这个问题上花了太长时间,但是:

C:\temp>notepad test_in.txt =>

Hello Java input!

您好 Java 输入!

In the same directory, create "Test.java":

在同一目录中,创建“Test.java”:

package com.mytest;

import java.io.*;

public class Test {

  public static void main (String [] args) throws IOException {
    System.out.println ("Current directory is " + new File(".").getAbsolutePath());

    System.out.println ("Reading file " + INPUT_FILE + "...");
    BufferedReader fis = 
      new BufferedReader(new FileReader(INPUT_FILE));
    String s = fis.readLine ();
    fis.close ();
    System.out.println ("Contents: " + s + ".");

    System.out.println ("Writing file " + INPUT_FILE + "...");
    PrintWriter fos = 
      new PrintWriter(new BufferedWriter(new FileWriter("test_out.txt")));
    fos.println ("Hello Java output");
    fos.close ();
    System.out.println ("Done.");
  }

  private static final String INPUT_FILE = "test_in.txt";
  private static final String OUTPUT_FILE = "test_out.txt";
}

Finally, run it - specify the full package name:

最后,运行它——指定完整的包名:

C:\temp>javac -d . Test.java

C:\temp>dir com\mytest
 Volume in drive C has no label.
 Volume Serial Number is 7096-6FDD

 Directory of C:\temp\com\mytest

05/17/2012  02:23 PM    <DIR>          .
05/17/2012  02:23 PM    <DIR>          ..
05/17/2012  02:29 PM             1,375 Test.class
               1 File(s)          1,375 bytes
               2 Dir(s)  396,478,521,344 bytes free

C:\temp>java com.mytest.Test
Current directory is C:\temp\.
Reading file test_in.txt...
Contents: Hello Java input!.
Writing file test_in.txt...
Done.

C:\temp>dir/od test*.txt
 Volume in drive C has no label.
 Volume Serial Number is 7096-6FDD

 Directory of C:\temp

05/17/2012  02:24 PM                17 test_in.txt
05/17/2012  02:29 PM                19 test_out.txt
               2 File(s)             36 bytes

'Hope that helps explain a few things, including:

'希望这有助于解释一些事情,包括:

  • Your "default directory" with respect to compiling and running

  • How "packages" relate to "directories"

  • The fact that Java will put your class files in the package directory (not necessarily your working directory)

  • 关于编译和运行的“默认目录”

  • “包”如何与“目录”相关

  • Java 会将您的类文件放在包目录(不一定是您的工作目录)中的事实

回答by Vlad

Eclipse looks for the file exactly in the root folder of the project (project name folder). So you can have src/test.in to locate file inside src folder of the project.

Eclipse 会在项目的根文件夹(项目名称文件夹)中查找该文件。因此,您可以使用 src/test.in 来定位项目的 src 文件夹中的文件。