1

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?

2 Answers 2

5

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" /> 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the follow up, I did a bit of reading and it looks like I want to use /res/raw, as this static data file would be distributed with the app rather than created by it (thus no /data/ directory)
0

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?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.