2

I am trying to read content of a file in java but I need to make it platform independent. So, I did

FileInputStream fis = new FileInputStream(new File(File.separator + "com" + File.separator + "test.txt"));
BufferedReader  br = new BufferedReader(new InputStreamReader(fis));

I am trying to run it in eclipse. But I am getting FileNotFoundExcption. Right now my test.txt is in same location as my source file. Please can anyone guide me through it. Where exactly is eclipse trying to look for this file? Thanks in advance..

Ishan Srivastava
  • 1,129
  • 1
  • 11
  • 29
Jeena
  • 309
  • 1
  • 5
  • 14
  • 2
    Are you sure the absolute path is /com/test.txt ? – Aubin Jun 13 '18 at 16:37
  • When eclipse builds the project, all non source files are also copied into the bin folder. By default, your application runs in the project folder. So the path will be bin//test.txt – Dakshinamurthy Karra Jun 13 '18 at 16:39
  • Yes that is what I want absolute path. But i dont know where will eclipse look for it? I mean is it pointing to my eclipse workspace/com/test.txt or c:/com/test.txt?? – Jeena Jun 13 '18 at 16:39
  • You can get Java to tell you where you are looking at by printing the file object's absolute path. – ifly6 Jun 13 '18 at 16:40

2 Answers2

3
File a = new File(File.separator + "com" + File.separator + "test.txt");
System.out.println(a.getAbsolutePath());

Will likely output something that you were not expecting.

If you post what that returns, I can tell you what your problem was/is.

It likely uses the project directory, which you can output using:

how to Locate the path of the Current project in Java, Eclipse?


Solution:

If the path on windows is:

C:\com\test.txt

But on linux you want:

/com/test.txt

You want to use:

new File(System.getenv("SystemDrive") + File.separator + "com" + File.separator + "test.txt")

As this will function as described above.

  • Super.. Thats what I wanted. Dont know why i didnt thought of that :) THank u so much you saved my day.... – Jeena Jun 13 '18 at 16:47
  • `File a = new File(File.separator + "com" + File.separator + "test.txt");` is already an absolute path, so printing `a.getAbsolutePath()` will give "/com/test.txt". – DodgyCodeException Jun 13 '18 at 16:53
  • @DodgyCodeException no... It won't. It will likely print `/com/test.txt` b/c I'm guessing OP is testing on windows instead of Linux. And on windows `/` dictates current working dir, not root. – Magic Octopus Urn Jun 13 '18 at 16:59
  • `System.getenv("SystemDrive");` will get you the default drive for the windows system (E.G. C:), and throws an exception/returns nothing on Linux boxes (I forget which, see API docs). You can use this to create an absolutePath on windows that also works on Linux because it will drop the drive prefix automatically if it detects none. On windows `new File("/").getAbsolutePath()` will print your project directory (if in eclipse) – Magic Octopus Urn Jun 13 '18 at 17:05
  • 1
    No, on Windows "/" means the **root directory** of the current drive. So if you run from `D:\one\two\three`, then `new File("/").getAbsolutePath()` will return D:\. – DodgyCodeException Jun 14 '18 at 08:59
  • @DodgyCodeException okay bud. You're wrong, but okay. If you read the question you'd see `I am trying to run it in eclipse.` but you didn't read it... Like you didn't read anything I said either. – Magic Octopus Urn Jun 14 '18 at 11:48
  • How did you come to the conclusion I hadn't read the question or anything you said? I read both. In Eclipse, you have a "default directory" in which each project runs, as you know. This is the directory I was referring to when I wrote "if you run from `D:\one\two\three`". You said in your comment "on windows `/` dictates current working directory", which is not true, so I offered you a correction. Again, to clarify, if you do `new File("/").getAbsolutePath()` in Eclipse, it will **not** return the Eclipse project directory; it will return the **drive** where the Eclipse project is, plus "\". – DodgyCodeException Jun 14 '18 at 12:09
2

Absolute version, probably not what you want:

BufferedReader br = new BufferedReader( new FileReader( "/com/test.txt" ));

Relative version:

BufferedReader br = new BufferedReader( new FileReader( "com/test.txt" ));

The Java File object, specialized by OS, translate this generic path in a platform dependent form.

The path is relative to the execution path, Eclipse has a small role: you may give the root path of execution in the Run/Debug dialog.

Aubin
  • 14,617
  • 9
  • 61
  • 84