我正在做一个 Android 项目,并且有一个 .bin 格式的文件,我将其放在 Java 项目的根目录中(不在任何文件夹中)。我尝试使用 FileInputStream 检索它,如下所示:
InputStream file = new FileInputStream("filename.bin");
但它无法读取该文件。
我应该把 bin 文件放在哪里?或者还有什么需要注意的吗?
[编辑]错误:
Error occurred during initialization of VM
java/lang/NoClassDefFoundError: java/lang/ref/FinalReference
[编辑]解决方案: 从这里找到解决方案>> Eclipse error: NoClassDefFoundError: java/lang/ref/FinalReference 。
1. Find Running configurations -> java application
2. In the new configuration's Classpath tab, find "Android Library" under Bootstrap Entries and remove it.
3. Still in the Classpath tab, select Bootstrap Entries and click the Advanced button.
4. Choose Add Library and click OK.
5. Select JRE System Library and click Next.
6. Select Workspace Default JRE and click Finish.
最佳答案
FileInputStream 还将文件放入其构造函数之一。
你可以做到
File f = new File("filename.bin")
if (f.isFile()) {
InputStream is = new FileInputStream(f);
}
else {
//cope with missing file
}
这样,如果文件消失或不在您认为的位置,您将不会收到 FileNotFoundException
@Srigan 提出了一个很好的观点,那就是尝试前面的/
关于java - fileinputstream 的文件位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12508096/