6

How to open a file that is not present in the current directory but in another directory. For example, I have a folder F:/test and my file is in F:/test/test2/doit.txt and D:/test3/doit2.txt

What to enter in path in parameter while making File object as follows :

File f = new File("/test2/doit.txt");
Tiny Jaguar
  • 433
  • 3
  • 12
  • 30

4 Answers4

8

Irrespective of which operating system, a file for example, demo.txt can be accessed like

File file = new File("/d:/user/demo.txt");

in Windows where the file is at D:\user\ and

File file = new File("/usr/demo.txt");

in *nix or *nuxwhere the file is at /usr/

Also, a file if wanted to be accessed relatively can be done as (considering the Windows example) :

Suppose I am in the songs directory in D: like:

D:/
|
|---songs/
|   |
|   |---Main.java
|
|---user/
    |
    |---demo.txt

and the code is inside Main.java, then the following code works.

File file = new File("../user/demo.txt");
TechSpellBound
  • 2,505
  • 6
  • 25
  • 36
5

Assuming that you are running your program from F:/test you should use something like:

File f = new File("./test2/doit.txt");

Using hardcoded absolute paths isn't a good idea - your program might not work when user has different directory structure.

Paweł Piecyk
  • 2,749
  • 15
  • 17
  • 1
    @NickJ explained on the accepted answer. If you cannot understand the comment, perhaps you should (do a bit of searching &) ask a new question. – Andrew Thompson Jan 25 '13 at 12:10
  • 1
    I've added more informations. @Andrew Thompson: The path is correct assuming that you're running program from D:/test and file location is D:/test/test2/doit.txt. – Paweł Piecyk Jan 25 '13 at 12:12
  • @AndrewThompson : I was actually directing Pawel to include the reson for his claims – Bhavik Shah Jan 25 '13 at 12:15
0

File inside of a project can be open as:

       File file = new File(path);

or

       File file = new File(./path);

where path is relative path from the project.

For example, when the project name is test and the file with name fileName is inside the test project:

      File file = new File("fileName");

or

      File file = new File("./fileName");
abdella
  • 490
  • 5
  • 11
-2

Please try the code below on Windows OS:

reader = new FileReader ("C:/Users/user/Desktop/java/test.txt"); 
kshetline
  • 12,547
  • 4
  • 37
  • 73
ssaitala
  • 125
  • 1
  • 1
  • 4
  • Ok, so the paths need to be absolute rather than relative in java.io. Thankx – Tiny Jaguar Jan 25 '13 at 11:30
  • 3
    The trouble with expressing the path like that, it makes sure that the application can only run on Windows. Java is supposed to be cross platform. Linux doesn't have a C: drive. – NickJ Jan 25 '13 at 11:32
  • 1
    Now I can read it, this is a poor answer. The OP should figure how to work with relative directories. Also, don't add salutations and sigs. into answers (or questions). As comments, they are noise. As part of a Q or A, really irritating noise. – Andrew Thompson Jan 25 '13 at 11:51