java - 把txt文件放在eclipse,android的哪里?

标签 java android eclipse

这是我需要访问 txt 文件“USSTATES.txt”的代码。

 public int count(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));

我应该将文件保存在硬盘的什么位置?我尝试将其放入 assets 文件夹和应用程序的根文件夹中,但在运行代码时仍然出现“No such file or directory”错误。

最佳答案

Where should I save the file in the hard drive? I tried putting it in assets folder and the root folder of the app, but I still get the "No such file or directory" error when I run the code.

因此,通常您有一些基于文件特征、文件内容、文件大小的选项。

  • 保存文件到sd卡
  • 将文件保存到 Assets 文件夹
  • 将文件保存到原始文件夹

例子:

SD 卡:

private final String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/filename.txt";
FileInputStream is = new FileInputStream(path);

Assets 文件夹:

InputStream is = getAssets().open("filename.txt");
BufferedInputStream bis = new BufferedInputStream(is);

原始文件夹:

InputStream is = getResources().openRawResource(R.id.filename);
BufferedInputStream bis = new BufferedInputStream(is);

建议:

如果您的文件较小,通常将其保存到 assets 文件夹或 raw 文件夹中。如果有更大的尺寸,例如 5-10+ MB,更好的解决方案是将其放入 SD 卡。

关于java - 把txt文件放在eclipse,android的哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15448687/

相关文章:

java - 在 Android 应用程序中如何从 google appengine 下载 blob

android - 什么是 posix_fallocate 的替代 API

android - Android 适配器是异步的吗?

android - 应用程序正在等待调试器附加

eclipse - 如何在 Java EE @ServerEndpoint 中获取 session.getRequestURI() 的端口

android - SherlockActionBar : Export signed apk, 然后 Eclipse 崩溃:(

java - 从 Android 应用程序进行 NFC 打印

java - Maven/SpringData/Neo4J : add Gremlin 1. 4

java - java中如何将日期时间转换为时间戳

android进度条问题