-1

I need some assistance with my Java project being unable to read my file that is in the same directory as my classes. Here is the snippet:

private static final String FILENAME = "my_address.txt";

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    AddressBook book = new AddressBook();
    book.readFile(FILENAME);
    int choice;
    do...

Whenever I try to bring in my text file (my_address.txt),I receive this message...

my_address.txt could not be found! Exiting..
Process finished with exit code 0

Can someone please assist me with getting my file onto my project? In the same file directory

As someone has mentioned in the comments, here is a snippet of my AddressBook portion:

package Animal;

public class AddressBook {
    private ArrayList<Person> people;

public AddressBook()
{
    this.people = new ArrayList<>();
}
public void readFile(String filename)
{
    Scanner fileReader;
    try
    {
        fileReader = new Scanner(new File(filename));
        while(fileReader.hasNextLine())
        {
            String[] data = fileReader.nextLine().trim().split(",");
            String firstName = data[0];
            String lastName = data[1];
            String address = data[2];
            String phone = data[3];
            people.add(new Person(firstName, lastName, address, phone));
        }
        fileReader.close();
    }catch(FileNotFoundException fnfe){
        System.out.println(filename + " could not be found! Exiting..");
        System.exit(0);
    }
}
Ceecode
  • 1
  • 2
  • You will need to show the code for AddressBook.readFile(String filename) as this is where things go wrong – morsor Oct 05 '21 at 05:08

1 Answers1

0

From the image linked to in your question, it looks like you are using IntelliJ as your IDE. So when you build your java source code, file my_address.txt gets copied to the same directory that contains the compiled class which in your case appears to be Main. So file my_address.txt should be in the same directory as file Main.class

The following line of code creates a path to a file.

new File(filename)

If the value of variable filename is my_address.txt then the path to the directory containing file my_address.txt will be the working directory. If you don't know what the working directory is, the following [java] code will get it for you.

String pathToWorkingDirectory = System.getProperty("user.dir");

You will find that it is not the same directory that contains the file Main.class and that's why you are getting the FileNotFoundException.

In your case, file my_address.txt is referred to as a resource and the JDK includes an API for retrieving resources.

Hence in order to fix your code such that it does not throw FileNotFoundException, either use the API for retrieving resources or move file my_address.txt to the working directory.

If you use the API, then the following java code shows how to create a Scanner for reading the file. Note that I assume that class Main is in the same package as class AddressBook, which, according to the code in your question, is package Animal. By the way, it is recommended to adhere to java naming conventions so the name of the package should be animal.

java.net.URL url = Main.class.getResource("my_address.txt");
if (url != null) {
    try {
        java.net.URI uri = url.toURI(); // throws URISyntaxException
        java.io.File f = new java.io.File(uri);
        java.util.Scanner fileReader = new java.util.Scanner(f); // throws FileNotFoundException
    }
    catch (java.net.URISyntaxException | java.io.FileNotFoundException x) {
        x.printStackTrace();
    }
}
else {
    // The file was not found.
}

The above code uses multi catch.

I also recommend printing the stack trace in the catch blocks in your code.

Abra
  • 19,142
  • 7
  • 29
  • 41