FileInputStream fstream = new FileInputStream("abc.txt")
在作为 jar 运行时抛出 FileNotFoundExceptionn。为什么 ?通常它能够在从 main 方法运行时找到。
最佳答案
class MyClass{
InputStream fstream = this.getClass().getResourceAsStream("abc.txt");
}
应该使用此代码。 并且文件(在本例中为 abc.txt)应保存在对象引用类位置。这意味着,this.getClass 指的是某个文件夹的位置,即 com/myfolder/MyClass.java 文件夹。
所以我们应该把abc.txt保存在com/myfolder这个位置。
关于java - 作为 jar 运行时出现 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41762124/