这是我需要访问 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/