1

I want to be able to read in a file, myFile.txt, that I ship with my app, either in the assets folder, the res/raw folder, where ever, it does not matter.

BUT! I do not want an InputStream of that file... I want to be able to do something like:

while ((reader = is.readLine()) != null) { ... } 

I've searched around and found stuff that was close but not really what I'm looking for..

Thank you for any help.

EDIT - some code I was trying:

InputStream in = this.mCtx.getResources().openRawResource(R.raw.myFile); Reader is = new BufferedReader(new InputStreamReader(in, "UTF8")); 

(now what? The Reader class doesn't have a readLine() method)

6 Answers 6

4

Just change

Reader is = new BufferedReader(new InputStreamReader(in, "UTF8")); 

to

BufferedReader is = new BufferedReader(new InputStreamReader(in, "UTF8")); 

BufferedReader has a readLine() method. You're unnecessarily upcasting to Reader and losing the additional features.

Sign up to request clarification or add additional context in comments.

2 Comments

awesome thanks! EDIT - I just noticed I'm required to throw an exception for this... Do you know under what circumstances this might fail?
Check out developer.android.com/reference/java/io/package-summary.html - you want to look at the descriptions for exceptions thrown in the constructors for both InputStreamReader and BufferedReader.
1

use BufferedReader instead of Reader for definition.

BufferedReader is = new BufferedReader(new InputStreamReader(in, "UTF8")); 

Bufferedreader has readline method but not reader

Comments

1

You can store the file in sdcard and specify its location in "file_path" in below code:

File selectedFile = new File("file_path"); FileInputStream fstream = new FileInputStream(selectedFile); BufferedReader reader = new BufferedReader(new InputStreamReader(fstream)); 

And then

while ((strLine = reader.readLine()) != null) { //Do Stuff Here } 

Comments

0

What about

BufferedReader reader = new BufferedReader(new BufferedInputStream(is)); 

Edit: Sorry - my fault. Just stick with the BufferedReader. Opening a ressource can be done with

InputStream is = getClass().getResourceAsStream("/org/example/res/my_file.txt"); 

See Class#getResourceAsStream)

1 Comment

How could I create the "is" object? From the InputStream? I'll update the OP with some code I was trying.
0

Use these steps to get it done....

File f = new File("/sdcard/MyFolder/MyFile.txt"); FileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); StringBuilder sb = new StringBuilder(); String str = new String(); while((br.readLine())!=null){ sb.append(br.readLine()); } str = sb.toString(); 

1 Comment

This will discard new lines as str will have no none. This probably isn't a good idea... either process each line directly or save the new lines for later processing.
0

I think you should use something like that:

BufferedReader in = new BufferedReader(new InputStreamReader(activity.getAssets().open("yourfile"))); 

StringBuilder buffer = new StringBuilder(); while ((line = in.readLine()) != null) buffer.append(line).append('\n');

And in your assets dir simply put your file without .txt extension. Hope this help you.

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.