Where is java looking for files?

I am 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 place "test.in" be? (src? bin ????)

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

+6
source share
5 answers

This is usually the exact location of the Java file that you are executing. Therefore, if you use your Java file in C: \ and look for a file without specifying a path, it will look in C: \, and the output file will also be in C: \

If you are using something like NetBeans, you just need to put the file in the NetBeans project folder of your specific project (root folder). Not in the source or bin folders of your project.

+3
source

The relative path'ed file is viewed in the directory specified by System.getProperty ("user.dir")

+9

- , , "java...". , eclipse.

+3

, , , :

C:\temp>notepad test_in.txt =>

, 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";
}

, - :

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

', , :

  • " "

  • "" ""

  • , Java ( )

+1

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

+1
source

All Articles