9

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?

Anonymous181
  • 1,863
  • 6
  • 24
  • 27
  • How are you executing your code? From the command line or from somewhere like eclipse? – dann.dev May 17 '12 at 21:14
  • Judging on the fact that you are asking about src and bin, I'm assuming you are using some sort of SDK? Otherwise the answer is trivial; the location will be the same location where you execute your java file. Provide a little more info about what SDK you're using and someone can answer better. – NominSim May 17 '12 at 21:14
  • 3
    "Where does Java look for files?" Why not ask Java itself: `System.out.println(System.getProperty("user.dir"));` Edit: 1+ to @mebigfatguy as I now see he gave the same suggestion before me. – Hovercraft Full Of Eels May 17 '12 at 21:23

5 Answers5

11

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

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
  • Thanks! But where is this noted in the Java documentation? – flow2k Oct 22 '18 at 20:03
  • Found it. https://docs.oracle.com/javase/8/docs/api/java/io/File.html. "By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked." – flow2k Oct 22 '18 at 20:08
4

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.

Hakan Serce
  • 11,198
  • 3
  • 29
  • 48
3

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.

olminkow
  • 121
  • 13
OmniOwl
  • 5,477
  • 17
  • 67
  • 116
  • 2
    This is only true if your current working directory also happens to be the same as the directory containing the "java file" (by which I assume we're talking about .class and .jar files). – rob May 17 '12 at 21:22
  • Funny it's the same result I get every time then. – OmniOwl May 17 '12 at 21:23
  • 1
    I updated my comment. If your class files or jarfiles are in `C:\java` and your current working directory (which you can see by using `System.out.println(System.getProperty("user.dir"));`) is `C:\run` when you launch the program, it will look for files in `C:\run`. – rob May 17 '12 at 21:27
  • 3
    -1. It is your current working directory when you execute the program. Period. – user207421 May 18 '12 at 00:16
  • I am pretty sure that's what I said, but okay. – OmniOwl May 18 '12 at 00:20
  • No, look at your first sentence. You're saying it's the location of the Java file. That is not correct, because if you're in a different directory than the Java program when you launch it, the default directory where files are loaded and saved will be different. There is a very distinct difference between what you are saying and what EJP and I are saying. – rob May 18 '12 at 19:04
1

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)

paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

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.

Kshitij Kulshrestha
  • 2,032
  • 1
  • 20
  • 27
Vlad
  • 11
  • 1