I am trying to use Java.util.scanner on a txt to parse it. I am having trouble trying to scan the file because the file isn’t found.
My class is within the same package of the txt file. So can you please tell me how to scan a file within the same package of the class.
public class PhoneBook {
private ArrayList<PhoneBookEntry>[] buckets;
public void load() {
try {
Scanner scan = new Scanner(new File("phone.txt"));
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
}
}
public static void main(String[]args) {
PhoneBook phone = new PhoneBook();
phone.load();
}
}
Here is the StackTrace of the error
java.io.FileNotFoundException: phone.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at HashSet.PhoneBook.load(PhoneBook.java:13)
at HashSet.PhoneBook.main(PhoneBook.java:23)
Put the file in the root of your class path (after compilation, your file should appear in the root folder where
.classfiles are generated) and use the code syntax below: