【问题标题】:File location for fileinputstream文件输入流的文件位置
【发布时间】:2012-09-20 07:43:25
【问题描述】:

我正在做一个 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.

【问题讨论】:

  • 失败时是否抛出异常?
  • 错误如上。这与我的 java 安装有关吗?
  • 哇。是的。类路径有问题或\并且您的整个 java 安装已损坏
  • 但是当我尝试 cmd line "java" 时它仍然可以。无论如何,我必须重新安装所有内容吗?恐怕它会影响我在eclipse中的项目。我该怎么做?

标签: java fileinputstream


【解决方案1】:

FileInputStream 还将一个文件放入其构造函数之一。

你可以的

File f = new File("filename.bin")
if (f.isFile()) {
    InputStream is = new FileInputStream(f);
} 
else {
    //cope with missing file
}

这样,如果文件消失或不在您认为的位置,您将不会收到 FileNotFoundException

@Srigan 提出了一个很好的观点,尽管尝试 /infront

【讨论】:

    【解决方案2】:

    上述语法表示文件filename.bin 与您的代码位于同一文件夹中。 尝试提供文件的绝对路径或将文件放入src 文件夹并使用

    InputStream file = new FileInputStream("/filename.bin");
    

    【讨论】:

      【解决方案3】:

      获取当前工作目录:System.getProperty("user.dir"));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-07
        • 1970-01-01
        相关资源
        最近更新 更多