简体   繁体   English

哪里把txt文件放在eclipse中,android?

[英]Where to put txt file in eclipse, android?

This is the code in which I need to access a txt file "USSTATES.txt". 这是我需要访问txt文件“USSTATES.txt”的代码。

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

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. 我尝试将它放在资源文件夹和应用程序的根文件夹中,但是当我运行代码时,我仍然得到“没有这样的文件或目录”错误。

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. 我尝试将它放在资源文件夹和应用程序的根文件夹中,但是当我运行代码时,我仍然得到“没有这样的文件或目录”错误。

So generally you have a few options based on character of file, content of file, size of file. 所以一般来说你有一些基于文件特征,文件内容,文件大小的选项。

  • Save file into sd card 将文件保存到SD卡中
  • Save file into assets folder 将文件保存到assets文件夹中
  • Save file into raw folder 将文件保存到原始文件夹

Examples: 例子:

SD card: SD卡:

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

Assets folder: 资产文件夹:

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

Raw folder: 原始文件夹:

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

Suggestions: 建议:

If your file has small size, normally save it either into assets folder or raw folder. 如果您的文件很小,通常将其保存到资源文件夹或原始文件夹中。 If has bigger size for example 5-10+ MB better solution is to place it into SD card. 如果有更大的尺寸,例如5-10 + MB更好的解决方案是将其放入SD卡。

You have to put your file in the assets folder 您必须将文件放在assets文件夹中

To access it you do it like that. 要访问它,你就是这样做的。

AssetManager am = getApplicationContext().getAssets();
InputStream is = am.open("file_name.txt");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM