我想包括文本文件中的资源文件夹到罐子文件。 这里是最小的例子:
import java.io.File;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Main main = new Main();
main.readFile( "test.txt" );
}
public void readFile(String fileName){
File file = new File( getClass().getResource( fileName ).getPath() );
try{
Scanner scanner = new Scanner( file );
while ( scanner.hasNextLine() ){
System.out.println( scanner.nextLine() );
}
}catch (Exception e){
System.out.println( e );
}
}
}
这里是什么test.txt 文件:
hello
world
这是我的项目看起来像:
我可以运行的代码成功。 我产生的罐子如下:
我已经增加资源的文件夹,通过点击"+"符号。 然而,当我呼吁所产生的罐子文件,它仍然抱怨FileNotFoundException.
任何建议?