简体   繁体   English

从android中的文本文件读取

[英]read from text file in android

I am new in android development, and I'm trying to create a simple application which reads some data from a text file and displays it in a ListView. 我是android开发的新手,我正在尝试创建一个简单的应用程序,该应用程序从文本文件读取一些数据并将其显示在ListView中。 The problem is my reader doesn't find my file. 问题是我的读者找不到我的文件。 I've debugged my application and that is the conclusion I've come up with. 我已经调试了应用程序,这就是我得出的结论。 So, where does the text file have to placed in order for the reader to find it? 那么,文本文件必须放置在哪里才能使读者找到它? Heres some code: 这是一些代码:

     try
    {
          FileInputStream fstream = new FileInputStream("movies.txt");
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));

          String strLine;

          while ((strLine = br.readLine()) != null)
          {
              filme.add(strLine);
              Log.d(LOG_TAG,"movie name:" + strLine);
          }
          in.close();
    }
    catch (Exception e)
    {
                System.err.println("Error: " + e.getMessage());
    }

Thanks! 谢谢!

Put the file named movies.txt in res/raw, then use the following code 将名为movies.txt的文件放入res / raw,然后使用以下代码

String displayText = "";
try {
InputStream fileStream = getResources().openRawResource(
                    R.raw.movies);
int fileLen = fileStream.available();
// Read the entire resource into a local byte buffer.
byte[] fileBuffer = new byte[fileLen];
fileStream.read(fileBuffer);
fileStream.close();
displayText = new String(fileBuffer);
} catch (IOException e) {
  // exception handling
}
FileInputStream fstream = new FileInputStream("movies.txt");

where is the path for movies.txt ?? movie.txt的路径在哪里? You must need to give the path as sd card or internal storage wherever you have stored. 无论存储在哪里,都必须将路径指定为sd卡或内部存储。

As if, it is in sd card 好像是在SD卡中

 FileInputStream fstream = new FileInputStream("/sdcard/movies.txt");

Usually when you want to open a file you put it into the res folder of your project. 通常,当您要打开文件时,将其放入项目的res文件夹中。 When you want to open a text file, you can put it into the res/raw directory. 当您要打开文本文件时,可以将其放入res / raw目录。 Your Android eclipse plugin will generate a Resource class for you containing a handle to your textfile. 您的Android eclipse插件将为您生成一个Resource类,其中包含文本文件的句柄。

To access your file you can use this in your activity: 要访问文件,您可以在活动中使用此文件:

InputStream ins = getResources().openRawResource(R.raw.movies);

where "movies" is the name of your file without the filetype. 其中“电影”是不带文件类型的文件名。

If you store your files on the SD card, then you can get the root of the SD card with Environment.getExternalStorageDirectory() . 如果将文件存储在SD卡上,则可以使用Environment.getExternalStorageDirectory()获取SD卡的根。

Note, that you might not be able to access the SD card, if it is mounted to the computer for example. 请注意,例如,如果SD卡已安装在计算机上,则可能无法访问它。

You can check the state of the external storage like this: 您可以像这样检查外部存储的状态:

boolean externalStorageAvailable = false;
boolean externalStorageWriteable = false;    

String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
    externalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    externalStorageAvailable = true;
    externalStorageWriteable = false;
} else {
    externalStorageAvailable = mExternalStorageWriteable = false;
}

if(externalStorageAvailable && externalStorageWriteable){
    File sdRoot = Environment.getExternalStorageDirectory();
    File myFile = new File(sdRoot, "path/to/my/file.txt");
}

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

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