NetBeans,尝试使用扫描程序读取.txt文件,FileNotFoundException

时间:2015-12-17 19:18:42

标签: java file text netbeans filenotfoundexception

我正在尝试从NetBeans IDE中读取文本文件中的字符以使用扫描程序填充对象数组。游戏的所有文件都在同一个文件夹src / wizard / game中。以下是我的GamePanel.java类,除了删除相关代码之外的所有内容:

public class GamePanel extends JPanel
{
    boolean instantiated;

    public GamePanel() {
        spaces = new Entity[width][height];
        // Tell whether entities have been instantiated yet or not
        instantiated = false;
    }

    // *** Interfacing methods ***
    // Instatntiate entites in a level from a text file
    public static void loadLevel(String l) throws FileNotFoundException {
        File level = new File(l);
        Scanner s = new Scanner(level);
        for(int y=1; y<=GamePanel.getH(); y++) {
            for(int x=1; x<=GamePanel.getW(); x++) {
                String nextIdentifier = s.next();
                switch(nextIdentifier) {
                    case("x"):
                        // Nothing
                        break;
                    case("s"):
                        new WallSandstone(x, y);
                        break;
                }
            }
        }
    }

    @Override
    public void paintComponent(Graphics g)
    {
        // Instantiate entities here only once
        while(instantiated == false){
            try {
                loadLevel("levelOne.txt");
            } catch (FileNotFoundException ex) {
                Logger.getLogger(GamePanel.class.getName()).log(Level.SEVERE, null, ex);
            }

            instantiated = true;
        }
    }
}

和levelOne.txt文件:

x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x s s s s s s s s s s s s s x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x s x x x x x x x x x x x s x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x s s s s s s x s s s s s s x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
s s s s s s s s s s s s s x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x s x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x s x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x s x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x s x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x s x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x s x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x s x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x s x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x s x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x s x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x s x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x

运行此代码会出现以下错误:

SEVERE: null
java.io.FileNotFoundException: levelOne.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.util.Scanner.<init>(Scanner.java:611)
    at wizard.game.GamePanel.loadLevel(GamePanel.java:65)
    at wizard.game.GamePanel.paintComponent(GamePanel.java:97)
    at javax.swing.JComponent.paint(JComponent.java:1056)

我做错了吗?我不能为我的生活弄清楚为什么这不起作用。

1 个答案:

答案 0 :(得分:0)

当您尝试加载文件时,可以尝试使用以下代码:

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("levelOne.txt").getFile());

如果您没有指定包,它将从类路径的根文件夹中查找fileOne.txt文件。

ie .. |-/GamePanel.java
      |-/levelOne.txt