File I/O error FileNotFoundException

1.9k views Asked by At

The following is the java code. I am trying to get the arrays from the problem1.txt and save it into the arraylist.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

class CSP {
    public int nMeetings;
    public static int nEmployees;
    public int nTimeSlots;
    public static int probNumber;
    public ArrayList<ArrayList<Integer>> nMeetingsPerEmployee;
    //public int[][] travelTime;

    public CSP(int nMeetings, int nEmployees, int nTimeSlots, int probNumber) throws FileNotFoundException {
        this.nMeetings = nMeetings; 
        this.nEmployees = nEmployees;
        this.nTimeSlots = nTimeSlots;
        this.probNumber = probNumber;
        this.nMeetingsPerEmployee = getnMeetingsPerEmployeeArrayList();

    }

    //gets nMeetingsPerEmployee Arrylist
    public static ArrayList<ArrayList<Integer>> getnMeetingsPerEmployeeArrayList() throws FileNotFoundException
    {
        ArrayList<ArrayList<Integer>> arraylist = new ArrayList<ArrayList<Integer>>();

        File openFile = null;
        if(probNumber == 1){
            openFile = new File("problem1.txt");
        }else if(probNumber == 2){
            openFile = new File("problem2.txt");
        }else if(probNumber ==3){
            openFile = new File("problem3.txt");
        }else
            System.out.println("File Not Found");

        Scanner in = new Scanner(openFile);

        for(int i=0; i<5; i++)
            in.nextLine(); //skip 5 lines until matrix

        while(true){
            for(int i=0; i<nEmployees; i++) //nEmployees = 33
            {
                for(int j=0; j<6 ;j++)
                {
                    for(int k=0; k<3; k++)
                        in.next(); //skip 3 characters until individual number
                    arraylist.get(nEmployees).set(nEmployees, Integer.parseInt(in.next()));
                    if(in.hasNext() == false);
                        break;
                }
            }

        return arraylist;
        }
    }
}

public class CSP_Main
{
    public static void main(String[] args) throws FileNotFoundException
    {
        CSP problem1 = new CSP(20, 33, 12, 1);
        //CSP problem2 = new CSP(20, 33, 12, 2);
        //CSP problem3 = new CSP(40, 62, 12, 3);
    }
}

The following is the error I keep getting.

Exception in thread "main" java.io.FileNotFoundException: problem1.txt 
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.util.Scanner.<init>(Scanner.java:611)
    at CSP.getnMeetingsPerEmployeeArrayList(CSP_Main.java:43)
    at CSP.<init>(CSP_Main.java:22)
    at CSP_Main.main(CSP_Main.java:70)

The following is the problem1.txt file that I am trying to read and save in the arraylist.

Number of meetings: 20
Number of employees: 33
Number of time slots: 12

Meetings each employee must attend:
1: 2   6   7   9  19
2: 2   5   6  12  16
3: 1   3   8   9  16
4: 1   6  15  16  18
5: 1   3   8  13  18
6: 8  10  11  17  20
7: 3   8  10  13  20
8: 1   3  14  16  20
9: 6   7   9  16  19
10: 2   6   7  12  17  19
11: 2   6   7   9  13
12: 2   4   7  12  16
13: 2   6   7   9  16  18
14: 2   5  11  17  18
15: 1   5  11  17  18  20
16: 6   8  12  16  18
17: 6   7  15  17  19
18: 1   7  11  18  20
19: 4   5   9  10  13
20: 4   7   9  17  18
21: 7  10  11  12  17
22: 5   6   9  13  18
23: 1   9  11  17  18
24: 2  11  14  15  17
25: 1   3  14  15  16
26: 5   6  10  13  15
27: 8  11  15  17  18
28: 1   4   7   9  16
29: 1  11  13  18  20
30: 2   5  12  13  18
31: 2   6   8  12  16
32: 2   7  15  17  19
33: 6   7  15  17  18

Travel time between meetings: 
      1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
1:    0  1  1  1  2  1  1  1  1  1  1  2  1  2  1  1  1  1  2  1
2:    1  0  1  1  2  2  1  1  1  1  1  1  1  1  1  2  2  1  1  2
3:    1  1  0  1  1  2  2  1  1  2  2  2  2  2  2  1  2  2  1  1
4:    1  1  1  0  1  2  2  2  2  2  1  1  1  2  2  1  1  2  2  2
5:    2  2  1  1  0  2  2  2  1  1  1  2  1  2  1  2  2  1  2  2
6:    1  2  2  2  2  0  1  1  1  1  2  1  2  1  2  1  1  2  2  1
7:    1  1  2  2  2  1  0  1  2  1  1  1  1  1  2  2  2  1  1  2
8:    1  1  1  2  2  1  1  0  2  1  1  2  1  1  2  1  2  1  2  1
9:    1  1  1  2  1  1  2  2  0  2  2  1  1  1  2  2  2  2  1  2
10:   1  1  2  2  1  1  1  1  2  0  1  2  2  2  2  2  1  1  2  2
11:   1  1  2  1  1  2  1  1  2  1  0  1  2  1  1  2  1  2  1  1
12:   2  1  2  1  2  1  1  2  1  2  1  0  1  2  2  2  2  1  2  2
13:   1  1  2  1  1  2  1  1  1  2  2  1  0  1  1  2  2  2  1  1
14:   2  1  2  2  2  1  1  1  1  2  1  2  1  0  2  1  1  2  1  1
15:   1  1  2  2  1  2  2  2  2  2  1  2  1  2  0  2  1  2  2  1
16:   1  2  1  1  2  1  2  1  2  2  2  2  2  1  2  0  1  1  2  2
17:   1  2  2  1  2  1  2  2  2  1  1  2  2  1  1  1  0  2  2  2
18:   1  1  2  2  1  2  1  1  2  1  2  1  2  2  2  1  2  0  1  1
19:   2  1  1  2  2  2  1  2  1  2  1  2  1  1  2  2  2  1  0  2
20:   1  2  1  2  2  1  2  1  2  2  1  2  1  1  1  2  2  1  2  0

The problem1.txt file that I am trying to open is surely in the directory where my java codes are. So I don't know why I keep getting this error.

Thank you for your time.

4

There are 4 answers

2
Rajesh On BEST ANSWER

ProjectRoot is working directory for Eclipse.

  • Move file above src directory i.e. to project root (Running from eclipse)

    openFile = new File("problem1.txt");
    
  • Provide path from src or bin to file location (Running from eclipse):

    openFile = new File("src/superbase/problem1.txt");
    
2
Rafiq On

You can see the bellow example.You will get file from same package.

     public class Hello {

      public static void main(String[] args) {
            Hello obj = new Hello();
            System.out.println(obj.getFile("file/p1.txt"));     
      }

      private String getFile(String fileName) {

            StringBuilder result = new StringBuilder("");

            //Get file from resources folder
            ClassLoader classLoader = getClass().getClassLoader();
            File file = new File(classLoader.getResource(fileName).getFile());

            try (Scanner scanner = new Scanner(file)) {

                    while (scanner.hasNextLine()) {
                            String line = scanner.nextLine();
                            result.append(line).append("\n");
                    }

                    scanner.close();

            } catch (IOException e) {
                    e.printStackTrace();
            }

            return result.toString();

      }

    }
0
Programmer On

This exception will is thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist. It is also thrown by these constructors if the file does exist but for some reason is inaccessible, for example when an attempt is made to open a read-only file for writing.

For more Information : https://docs.oracle.com/javase/7/docs/api/java/io/FileNotFoundException.html

1
Programmer On

This is how you can give correct path you did not add backslash and also backslash should be two because one backslash is consider as escape sequence.

File file = File("C:\\abcfolder\\textfile.txt");