【问题标题】:File not found issue in AndroidAndroid中找不到文件问题
【发布时间】:2012-02-29 23:10:11
【问题描述】:

我需要在 Android 中使用 FileReader 访问 myfile.txt 文件,请建议我在 Eclipse 中添加文本文件的位置。我尝试将它添加到资源和资产中,但我遇到了 File not found 问题。

FileReader fr = new FileReader("myfile.txt");

偶数

File ff = new File("myfile.txt");

文件仅支持以下列出的参数

FileReader 仅支持以下列出的参数

注意:我想要解决这个问题,只能使用 FileReader 或 File

【问题讨论】:

标签: android eclipse


【解决方案1】:

目录将是/res/raw/,这是您放置所有额外资源的地方。 你可以参考它使用getResources().openRawResource(resourceName) 并在这里查看Android how to get access to raw resources that i put in res folder?

编辑:

how can i edit the text files in assets folder in android 简而言之 最简单的方法是将文件复制到外部目录然后在那里做你的东西 链接在这里 Android: How to create a directory on the SD Card and copy files from /res/raw to it?

有一点需要提一下 - 在 2.3 之前,资产中的文件大小不能超过 1MB。


希望对你有所帮助

【讨论】:

  • 但 FileReader 或 File 不接受 InputStream 作为参数,getResources().openRawResource(resourceName) 返回 InputStream
  • 我会在几分钟内更新我的答案。这显然是错误的。
【解决方案2】:

这就是我从 SD 卡中获取文件的方式,也许这对你有用。

String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
        File options = new File(getAppDirectory(), "portal.xml");
}

这段代码中使用的 getAppDirectory 方法如下所示:

private String getAppDirectory() {
    return new String(Environment.getExternalStorageDirectory().toString() 
            + "/foldername/foldername/");
}

在这段代码之后,我还在尝试读取文件之前确保文件存在以及不存在。

【讨论】:

  • 你在 getAppDirectory() 方法中有什么?
  • 更新了我的答案,对此感到抱歉;)
  • 谢谢,但我想将它放在应用程序包中,并希望在我的应用程序启动时使用它
  • 那么我建议您查看AssetManager 类并将您的文档放在Android 项目中的/assets 中。
猜你喜欢
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 2013-10-27
  • 1970-01-01
  • 2016-01-19
  • 1970-01-01
  • 2013-09-18
  • 1970-01-01
相关资源
最近更新 更多