0

I'm trying to read from a .txt file that is located in src\main\java\props\engprops.txt. I'm using backslashes here because I'm using Windows.

When I try to read the file I get a FileNotFoundException.

This is my helper class. It contains a method for returning a string representation of the directory in which the file is located. Here is what it returns C:\Users\SOME_USER\workarea\Myapp\myapp\src\main\props\

private final static String APP_HOME = "\\workarea\\Translate\\translate\\src\\main";

private final static String PROPS_HOME = "\\props\\";

public static String getPropsPath() {
    StringBuilder sb = new StringBuilder();
    String userHome = System.getProperty("user.home");
    //userHome = userHome.replace("\\", "/");
    String propsHome = null;
    propsHome = userHome + APP_HOME + PROPS_HOME;

    return propsHome;
}

Then in my main class is where I try to read the file:

private static String stringPath = AppHelper.getPropsPath();

public static void main(String[] args) {
    readFile();
}

public static void readFile() throws IOException {
    FileReader fReader = new FileReader(stringPath + "engprops.txt");
    BufferedReader bReader = new BufferedReader(fReader);

    String line = null;

    while((line = bReader.readLine()) != null) {
        System.out.println(line);
    }

    bReader.close();
}

Inside the readFile() method, I am appending the name of the file on to the stringPath variable with the file name being engprops.txt.

Here is a snippet of the console displays after the program executes; java.io.FileNotFoundException: C:\Users\600010209\workarea\Translate\translate\src\main\props\engprops.txt (The system cannot find the file specified)

cod3min3
  • 585
  • 1
  • 7
  • 23
  • You don't need to use backslash because you're on Windows, you can use normal slashes, so that your app doesn't have OS dependency, Windows accepts it too. Also, check this question: http://stackoverflow.com/questions/1464291/how-to-really-read-text-file-from-classpath-in-java – Julian Jul 22 '16 at 16:41
  • In these situations, always assume that you've gotten something wrong, not the JVM. Try to create a new File object and hard-code the path you have in. Check file.Exists and file.canRead. In windows explorer, navigate to the file and confirm you aren't missing a directory. Be case sensitive, even with the file extension (sometimes you get those weird .TXT files; that could be causing an issue). – Matt Coats Jul 22 '16 at 16:48
  • When you see an exception like that, the first thing you should is copy the filename from the exception message, and paste it into a `dir` command in a command window: `dir C:\Users\600010209\workarea\Translate\translate\src\main\props\engprops.txt` – VGR Jul 22 '16 at 17:04

1 Answers1

2

You wrote in your question that your file was located under src/main/java/props, but the props path you declared was under src/main/props.

Comment if that doesnt answer your question.

Dastur
  • 684
  • 6
  • 23
  • Current I have the file located in `java\props`. At the beginning of the question I stated the full path. Can you elaborate further on what you meant please? – cod3min3 Jul 22 '16 at 16:51
  • You say you have the file in `java\props`. But it appears that `AppHelper.getPropsPath();` is returning `main\props`. Either move the file to that folder, or change AppHelper to return the correct folder. – FredK Jul 22 '16 at 17:10