0

I am trying to read in data from a file /sampledata/delivererList.txt and I cannot get the file to open and receive an No file or directory error each time even though it very much exists. For context im trying to retrieve information on a deliverer based on whether they are eligible to deliver, 1 or 0, and pass the information back to main function within a String array.

sampledata/delivererList.txt

1,deliverer name,666-666-6666,test
0,different deliverer,999-999-9999,alsoTest

read function

public String[] readDeliverers() {
        String[] noDeliverer = {"0", "NO", "DELIVERER", "AVAILABLE"};
        try {
            File deliverFile = new File("sampledata/delivererList.txt");
            Scanner fileReader = new Scanner(deliverFile);
            if (fileReader.next().charAt(0) == '1') {
                String data = fileReader.nextLine();
                List<String> delivererList = Arrays.asList(data.split(","));
                String[] delivererArray = new String[delivererList.size()];
                int index = 0;
                for (String value : delivererList) {
                    delivererArray[index] = value;
                    index++;
                }
                return delivererArray;
            } else {
                System.out.println("Error read");
            }
            fileReader.close();
        } catch (FileNotFoundException e) {
            System.out.println("An error occurred. NO FILE");
            e.printStackTrace();
        }
        return noDeliverer;
}
Kyle Boan
  • 3
  • 2
  • Please edit your question to include the error you get. – Luke Woodward Oct 05 '22 at 19:42
  • 1
    I suspect your root destination is different from the one you expect. Correct me if I'm wrong but you are expecting this file to be looking into a folder which contains a folder called "sampledata", correct? It might be the case that the root folder is starts in is different to you expect. I think this might help: https://stackoverflow.com/questions/3844307/how-to-read-file-from-relative-path-in-java-project-java-io-file-cannot-find-th – Vorkos Oct 05 '22 at 20:27

0 Answers0