简体   繁体   English

NetBeans和Eclipse FileNotFoundException

[英]NetBeans and Eclipse FileNotFoundException

I'm new in NetBeans and have problems with both IDEs not finding my files. 我是NetBeans的新手,并且在两个IDE都找不到我的文件时遇到问题。 If i compile and run my programm in PowerShell it runs perfectly. 如果我在PowerShell中编译并运行我的程序,它运行完美。 The ExceptionError loks like this "java.io.FileNotFoundException: model\\field\\levels\\map01.field". ExceptionError就像这样“java.io.FileNotFoundException:model \\ field \\ levels \\ map01.field”。 My packages are. 我的包裹是。

  • controller 调节器
  • model.field.levels model.field.levels
  • view.tiles view.tiles

In Field.java: 在Field.java中:

public void setField() {
    FieldReader fr = new FieldReader();
    field = fr.load("model/field/levels/map01.field");
}

In FieldReader.java: 在FieldReader.java中:

public int[][] load(String path){
    int field[][] = null;
    try {
        File file = new File(path);
        BufferedReader br = new BufferedReader(new FileReader(file));
        String s = null;
        int line = 0;
        int x = 0;
        int y = 0;
        while((s = br.readLine()) != null) {
            if(line == 0) {
                int start = 0;
                int split = 0;
                for (int i = 0; i < s.length(); i++) {
                    if(s.charAt(i) == 32) start = i + 1;
                    if(s.charAt(i) == 'x') split = i;
                }
                x = Integer.parseInt(s.substring(start, split));
                y = Integer.parseInt(s.substring(split + 1));
                System.out.println(x);
                System.out.println(y);
                field = new int[x][y];
            }
            if(line >= 2) {
                for (int i = 0; i < x; i++) {
                    field[i][line - 2] = Character.getNumericValue(s.charAt(i));
                }
                System.out.println(s);
            }
            line++;
        }
        br.close();
    } catch ( IOException e ) {
        e.printStackTrace();
    }
    return field;
}

The file is in place and by loading other files like images everything works fine. 该文件已就位,通过加载其他文件,如图像一切正常。

The Problem is allready solved. 问题已经解决了。 In NetBeans it is possible to set the working direktory while running under right clicking the Prokekt > Properties > Run > Working Direktory. 在NetBeans中,可以在右键单击Prokekt> Properties> Run> Working Direktory的情况下运行时设置工作的direktory。 Getting the absolutepath leads me to solution, so thanks for the hint Thorbjorn. 获得绝对路径会让我找到解决方案,所以感谢提示Thorbjorn。 :D :d

You are relying on relative paths, which require current working directory to be set to something useful. 您依赖于相对路径,这需要将当前工作目录设置为有用的东西。 Java doesn't do this automatically so you (or your IDE) need to do this when launching your program. Java不会自动执行此操作,因此您(或IDE)在启动程序时需要执行此操作。 A very useful tool is 一个非常有用的工具是

File f = ....
System.out.println(f.getAbsolutePath());

to tell you what is actually being looked for. 告诉你实际上正在寻找什么。 When you get more advanced you can ask the JVM where it found your class and then deduce the location of the files you need. 当您获得更高级的时候,您可以向JVM询问它在哪里找到您的课程,然后推断出您需要的文件的位置。

Note however that when packaging java programs everything is packed inside a jar file. 但请注意,在打包java程序时,所有内容都打包在一个jar文件中。 Then your current approach will not work. 那你当前的方法是行不通的。 You should look into resources which can be found on the class path easily regardless of whether you are in your IDE or run in production (, but where the files cannot easily be seen by other programs). 您应该查看可以在类路径中找到的资源,无论您是在IDE中还是在生产中运行(但是其他程序都不能轻易看到这些文件)。 This is a very common problem new programmers run into so you might as well start now instead of reworking your program later. 这是新程序员遇到的一个非常普遍的问题,因此您现在可以从现在开始,而不是稍后重新编写程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM