开发者

Read file with Android, what is the expected path?

开发者 https://www.devze.com 2023-04-11 16:14 出处:网络
I am not having luck with this method, which is basically a hello world for printing file contents to the Android cat log.

I am not having luck with this method, which is basically a hello world for printing file contents to the Android cat log.

  try {
            InputStream instream = openFileInput("inputFile.txt");
              InputStreamReader inputreader = new InputStreamReader(instream);
              BufferedReader buffreader = new BufferedReader(inputreader);
              String line;
              while (( line = buffreader.readLine()) != null) {
                System.out.println(line);
              }
  开发者_如何学运维          instream.close();
          } catch (java.io.FileNotFoundException e) {
            e.printStackTrace();
          }

Using a default generated project with the Android Eclipse plugin, under what directory should this file exist? Any other considerations?


This will try to read the file inputFile.txt from your internal application data directory /data/data/your.application.package/ (and will fail if it doesn't exist):

openFileInput("inputFile.txt");

To read a file from SD card you would do something like this:

new FileInputStream(Environement.getExternalStorageDirectory()
    .getAbsolutePath() + "/inputFile.txt")

Don't forget to set the SD card permission in your manifest then:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


This file should exist in the apps local 'data' directory. Does this file already exist? Have you taken a look at the documentation for file IO on Android?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号