简体   繁体   中英

Where does java look for files?

I'm trying to read a file in 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? ???)

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

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

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.

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:\\

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.

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

C:\temp>notepad test_in.txt =>

Hello Java input!

In the same directory, create "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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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