我是否正确使用Scanner和File类?

时间:2019-03-29 20:51:30

标签: java file java.util.scanner

我正在使用java.util.Scannerjava.io.File创建一个接受用户输入模式的生活游戏程序

主要问题是我似乎无法获得程序来读取pattern.txt文件...

我看不到任何问题,编译它们时pattern.txt.java.class文件位于同一文件夹中。

我正确使用了FileScanner吗?

我尝试重新排列import语句,更改trycatch的结构,并创建一个新的File(//..Path/../pattern.txt)以通过相对路径直接调用文件

1.txt是特征码文件:

20 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

`

    import java.util.Scanner;
    import java.io.File;
    import java.io.FileNotFoundException;

    public static void main(String [] args) throws FileNotFoundException{

    String inputFileName = "1.txt"; // Directly setting relative path

    int [][] initStates = getInitialStates(inputFileName);

    Grid gd = new Grid(initStates);

    gd.display();
    gd.getStats();
    gd.run();
    }

    public static int [][] getInitialStates(String fileName) {
    try {
        File file = new File(fileName);
    // checks to see if file was created
    //            if (file.exists())
    //                System.out.println("FILE EXISTS");
    //            else
    //                System.out.println("FILENOTFOUDN");
        Scanner input = new Scanner(file);     // Create scanner to read file
        int[][] states = null;
        int rows = input.nextInt();                   // get rows and cols values from first values of text file
        int cols = input.nextInt();
        // states = new int[rows][cols];           // Create 2d array w/ rows and cols
        states = new int[rows][cols];
    //        for (int i = 0; i < rows; i++) {
    //            states[i] = new int[cols];
    //        }

        // Read initial states from the input file
        for (int i = 0; i < states.length; i++) {
            for (int j = 0; j < states[0].length; j++) {
                states[i][j] = input.nextInt();
            }
        }
        return states;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    }
    //        return states;
}

`

错误输出是FileNotFoundException打印堆栈跟踪:

java.io.FileNotFoundException: 1.txt (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.util.Scanner.<init>(Scanner.java:639)
at client.getInitialStates(client.java:67)
at client.main(client.java:24)
Exception in thread "main" java.lang.NullPointerException
at Grid.<init>(Grid.java:14)
at client.main(client.java:26)

我收到NullPointerException,因为getInitialStates()方法未返回2d数组,因为它无法读取文件。

1 个答案:

答案 0 :(得分:0)

如果根据注释抛出FileNotFoundException,则表示程序的工作目录(即调用java YourMainClassName的目录)与程序的位置不同。您要按其名称打开的文件。

您可以检查工作目录:

System.out.println("Working Directory = " + System.getProperty("user.dir"));

如本答案所述:https://stackoverflow.com/a/7603444/1083697