Netbeans - Reading a data file in src folder

前端 未结 3 1103
旧时难觅i
旧时难觅i 2020-12-11 17:27

I have a scanner that\'s trying to read a file named info.data in the src folder.I get Exception in thread \"main\" java.io.FileNotFoundException: info.data (The system cann

3条回答
  •  既然无缘
    2020-12-11 17:41

    If the input file is always part of your application (i.e. you also put this into the .jar file later) you should use getResourceAsStream() in order to read its contents.

    InputStream in = getClass().getResourceAsStream(filename);
    Scanner scanner = new Scanner(in);
    

提交回复
热议问题