文件I / O错误FileNotFoundException

时间:2015-06-07 12:17:55

标签: java io backtracking

以下是java代码。我试图从problem1.txt获取数组并将其保存到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);
    }
}

以下是我不断得到的错误。

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)

以下是我试图阅读并保存在arraylist中的problem1.txt文件。

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

我试图打开的problem1.txt文件肯定在我的java代码所在的目录中。所以我不知道为什么我一直收到这个错误。

感谢您的时间。

4 个答案:

答案 0 :(得分:0)

ProjectRoot是Eclipse的工作目录。

  • 将文件移到src目录上方,即项目根目录(从eclipse运行)

    openFile = new File("problem1.txt");
    
  • 提供从src或bin到文件位置的路径(从eclipse运行):

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

答案 1 :(得分:0)

您可以看到以下示例。您将从同一个包中获取文件。

     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();

      }

    }

答案 2 :(得分:0)

当具有指定路径名的文件不存在时,FileInputStream,FileOutputStream和RandomAccessFile构造函数将抛出此异常。如果文件确实存在但由于某种原因无法访问,例如当尝试打开只读文件进行写入时,这些构造函数也会抛出它。

更多信息:https://docs.oracle.com/javase/7/docs/api/java/io/FileNotFoundException.html

答案 3 :(得分:0)

这是你可以给出正确的路径,你没有添加反斜杠,反斜杠应该是两个,因为一个反斜杠被认为是转义序列。

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