开发者

Read file with Android, what is the expected path?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜