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
- objects
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