0

I'm trying to pull data from a text file which is in my project but under a different package. This is the layout of the project: Project Layout

When I use the path it returns an error, but when I call it via location it works correctly. I'm using Eclipse.

This is the error message I get: Error

package park.FILEMOD;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class FileModifier {

    public static void main(String[] args) throws IOException{
        
        FileReader in = new FileReader("/park_mp1/src/park/DATA/Data1.txt");
        BufferedReader br = new BufferedReader(in);

        String line;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        in.close();

    }
    
}
Alias Cartellano
  • 366
  • 1
  • 3
  • 12
  • 4
    Note that the path `/park_mp1/src/park/DATA/Data1.txt` will only work within eclipse because the minute you compile and export the src file no longer exists. Consider loading the file [as a resource](https://stackoverflow.com/questions/15749192/how-do-i-load-a-file-from-resource-folder) or using a relative path. – sorifiend Oct 19 '21 at 23:41

1 Answers1

0

Easy way:

-- In Eclipse, I had to change the line to:

FileReader in = new FileReader("src/park/DATA/Data1.txt");

Why this works? Because IDE's accept either the full-path starting from the root, e.g. "/Users/xyz/abc/park_mp1/DATA/Data1.txt" or for the programmer's ease of use, relative path. Every IDE can define how it wants to make life easy for you. Some IDE's can also accept what you had written. What they do is in the back, they are just replacing it with the full path.

Where it does not work? Go to Terminal, and do it using the Terminal. Do javac FileModifier.java, then java FileModifier Use the same line which works for Eclipse:

FileReader in = new FileReader("src/park/DATA/Data1.txt");

Now, this does not work, same error as you got previously. So, in the end, FileReader just needs the Data1.txt file (while using the terminal, you would use FileReader in = new FileReader("../DATA/Data1.txt");, and you just have to provide it a path to get there. Now for eclipse, it says go like "src -> park -> ..." and Terminal asks to go like ".. -> DATA -> ..."

Why? Because Terminal knows that "../" means go-up a directory. Eclipse can implement the same thing, it just does not, due to some reasons which require further research.

Kushal Kumar
  • 174
  • 1
  • 13
  • When I switch out /* FileReader in = new FileReader("/park_mp1/src/park/DATA/Data1.txt"); */ for /* FileReader in = new FileReader("src/park/DATA/Data1.txt"); */, I still get the same error – dylanpark01 Oct 20 '21 at 00:56
  • @dylanpark01 Read through the answer again, the end section gives the answer. For this solution you need something like `FileReader in = new FileReader("park/DATA/Data1.txt");`, however, note that this still has issues when the files are packaged inside your jar, in which case you should be loading your files similar to how you [load a resource](https://stackoverflow.com/questions/15749192/how-do-i-load-a-file-from-resource-folder), but you would use the correct path to your package. – sorifiend Oct 20 '21 at 01:16
  • Not Eclipse executes the code, but the Java VM. A relative path (not starting with `/`) will be resolve by the Java VM relative to the working directory. The working directory is by default the project directory and can be changed in the launch configuration in the _Arguments_ tab. – howlger Oct 20 '21 at 16:42