[英]FileInputStream Javafx in .jar files
I am creating a Javafx application in Intelij and FileInputStream
works perfectly. 我正在Intelij中创建Javafx应用程序,
FileInputStream
可以完美运行。 However, when I create a .jar file from the project and try to run it the code fails to run as it is unable to locate the file in the file input stream. 但是,当我从项目中创建一个.jar文件并尝试运行它时,由于无法在文件输入流中找到该文件,因此代码无法运行。 Here is my code:
这是我的代码:
ObjectInputStream os = new ObjectInputStream(new FileInputStream("src/settingStorage.bin"));
Am I doing something wrong? 难道我做错了什么?
There is always some issue accessing a file outside a jar, depending on where the file location is. 在jar之外访问文件始终存在一些问题,具体取决于文件位置。 You can check this SO question/answer to have an idea on accessing your file.
您可以检查该SO问题/答案以了解有关访问文件的想法。
Try this: 尝试这个:
EDIT: Look at this again, I left part out. 编辑:再看一次,我遗漏了一部分。
ObjectInputStream os = new ObjectInputStream(new FileInputStream(SomeClass.class.getResourceAsStream(“settingStorage.bin")));
This usually works for me. 这通常对我有用。 When I run this, it operates fine, in AND out of the development area.
当我运行它时,它可以很好地在开发区域之外运行。 Don't include /src, as when you call getResourceAsStream off a class, it already checks inside the jar.
不要包含/ src,因为当您从类中调用getResourceAsStream时,它已经在jar中进行了检查。
Cheers! 干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.