0

I'm currently trying to work on structuring my packages and files in my Java programs but I am having issues which I cannot fathom.

Here are the relevant parts of my code:

public static final String fileDirectory = "../txtFiles";

public static ArrayList<Account> readAccounts()
    throws Exception
    {
        // The name of the file which we will read from
        String fileName = "bank_details.txt";
        int accNum;

        // Make an ArrayList to store all the accounts we will make
        ArrayList<Account> accounts = new ArrayList<Account>();

        File file = new File(fileDirectory, fileName);
        Scanner input = new Scanner(file).useDelimiter(",$");
    }

My directory structure looks like this:

  • Three
    • objects
      • Account.java
    • txtFiles
      • bank_details.txt
    • run
      • ReadingFiles.java

I'm trying to access bank_details.txt from ReadingFiles.java, which has the directory structure "..\txtFiles\bank_details.txt" but the method I am using is not working. Instead I get a FileNotFoundException error and the error message "The system cannot find the path specified".

I used a very similar method to access files in other programs I have written, including "\..\" to access higher directories, so I cannot understand why this is not working.

Any help would be appreciated.


EDIT:

I'm compiling everything via the command prompt.

I compile it from the directory above Three, compiling the objects first:

javac Three\objects\*.java

Then I compile the main program:

javac -classpath .\Three run.ReadingFiles.java

SOLUTION:

I have changed this:

public static final String fileDirectory = "../txtFiles";

to this:

public static final String fileDirectory = "Three/txtFiles";

Down to my lack of understanding of how java interacts with the working directory in the end. Thanks for the help all

  • 1
    Is the application Jared? Does it contain the contents of `txtFiles`? – MadProgrammer Oct 20 '13 at 23:28
  • Where are the compiled class files? How do you run the application (in particular: with which working directory)? – Thilo Oct 20 '13 at 23:30
  • I'm compiling everything via the command prompt. I compile it from the directory above Three, compiling the objects first: javac Three\objects\\*.java Then I compile the main program: javac -classpath .\Three run.ReadingFiles.java – Zach Beavon-Collin Oct 20 '13 at 23:31
  • The error message you are getting means that when the program runs it doesn't see the files you are looking for. I'd recommend googling how to get the current path and output it, then that should help you verify if your path reference is correct (or more likely why it is not). – James Oravec Oct 20 '13 at 23:33
  • I've already searched for an answer but I am pulling no dice. It's especially confusing as I have done something syntactically identical before and it has worked perfectly – Zach Beavon-Collin Oct 20 '13 at 23:38
  • You should look at [how Java uses resources](http://stackoverflow.com/questions/2343187/loading-resources-using-getclass-getresource). – Maarten Bodewes Oct 20 '13 at 23:39
  • I am not so sure about your folder structure. I don't see any packages mentioned. It seems you are mixing up code and input files as well. You should either view your data as (static) resource, or something that is user input. In the resource view, you should access your files relative to the location of your `.class` files, in the case of user input, they are relative to your current working directory. Also, I presume you mean "Tree" instead of "Three"? – Maarten Bodewes Oct 20 '13 at 23:47
  • Your path is wrong. You're running the class from the project's top-level directory; moving up a level from there puts you in the wrong directory. It'd be a lot easier to use resources as streams instead of absolute file paths, or to specify the path on the command line. – Dave Newton Oct 20 '13 at 23:48
  • What is the output of `file.getAbsolutePath()`? – cnnr Oct 20 '13 at 23:51
  • The trouble is that I have tried both "\..\txtFiles\bank_details.txt" and "\txtFiles\bank_details.txt". Surely if the trouble was the fact that I was running the program from the root directory "Three\" then the second version would fix the issue. I have named the root directory "Three" on purpose. And the program is not running past the file declaration so I couldn't run file.getAbsolutePath() – Zach Beavon-Collin Oct 21 '13 at 00:40
  • `new File()` shouldn't throw an exception in this case even if the path is wrong (you can create `File` objects for paths that don't exist). Without having seen your stack trace, I think the exception is being thrown by `Scanner` on the next line. Try inserting a `System.out.println(file.getAbsolutePath())` in between. – cnnr Oct 21 '13 at 01:16
  • Ah nevermind, looks like you figured it out. :) – cnnr Oct 21 '13 at 01:17

0 Answers0