0

Well I understand that this question has been asked multiple times, but despite following all the advice in those links I still end up getting file not found error.

These are the links I've already checked.

Where do I put the txt file that I want to read in Java?

How to really read text file from classpath in Java

So please don't get trigger happy marking it as duplicate.

For one most of these links seem to use the word ClassPath and BuildPath interchangeably, are they the one and the same thing, coz in my IDE I can't seem to find anything related to ClassPath, it's only BuildPath.

So this is my directory structure.

enter image description here

The caller class needs the file

enter image description here

Well I've tried pretty much everything in the links and I still can't read the file.

The code that reads the file is here

String path = "DataSource/data1.txt";
        String jsonData = null;

        try {

            jsonData = Reader.readFile(path);
        } catch (Exception e) {
            e.printStackTrace();
        }

Any help appreciated.

Community
  • 1
  • 1
Zeus
  • 2,213
  • 8
  • 28
  • 45
  • 1
    So where's the code? – Codebender Apr 01 '16 at 14:07
  • Non-java files don't belong in ``src/main/java`` but in ``src/main/resources``. Also: "I still can't read the file." - what error are you getting? – f1sh Apr 01 '16 at 14:10
  • I,m getting the following error `java.io.FileNotFoundException: DataSource/data1.txt (No such file or directory)` – Zeus Apr 01 '16 at 14:13
  • @Sombriks has the right answer. You should almost never use te java.io.File class when build web applications. Always work with streams. – markbernard Apr 01 '16 at 14:19

2 Answers2

2

try

String path = "src/main/java/DataSource/data1.txt";

understand that your execution point inside eclipe is at project root.

However you might want to package such resource on your jar. if so, you'll need to use something like:

jsonData = Reader.readFile(Caller.class.getResourceAsStream("DataSource/data1.txt""));

At last, move the .txt file to 'src/main/resources', since it's a good practice.

Sombriks
  • 3,370
  • 4
  • 34
  • 54
  • The first one works, is there a way I can use the second method to consume a String as opposed to an input stream. – Zeus Apr 01 '16 at 14:27
  • What I mean is my reader uses a string parameter not an inputStream. – Zeus Apr 01 '16 at 14:28
  • Hello @Zeus, there are other options, this one for exemaple: https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAllBytes(java.nio.file.Path) you read the bytes and then do a new String with those bytes. however the .jar file limitation remains. If, at some point, your resource get packaged, only classpath resources can help you. – Sombriks Apr 01 '16 at 17:11
0

Term ClassPath related to java code compilation and execution while term BuildPath related to IDE (i.e. Eclipse). IDE adds libraries, which are added to BuildPath, automatically to the ClassPath while building and running the application/program.

"The caller class needs the file", then you can provide the file-path in two way

  1. Provide the absolute file path.
  2. Provide relative path, which you are doing, but make sure it is relative to the current location from where you are running your program. In case of IDE relative path should be from Project-Directory.
Mahendra
  • 1,436
  • 9
  • 15