[java] Convert InputStream to BufferedReader

I'm trying to read a text file line by line using InputStream from the assets directory in Android.

I want to convert the InputStream to a BufferedReader to be able to use the readLine().

I have the following code:

InputStream is;
is = myContext.getAssets().open ("file.txt");
BufferedReader br = new BufferedReader (is);

The third line drops the following error:

Multiple markers at this line
The constructor BufferedReader (InputStream) is undefinded.

What I'm trying to do in C++ would be something like:

StreamReader file;
file = File.OpenText ("file.txt");

line = file.ReadLine();
line = file.ReadLine();
...

What am I doing wrong or how should I do that? Thanks!

This question is related to java android inputstream readline bufferedreader

The answer is


A BufferedReader constructor takes a reader as argument, not an InputStream. You should first create a Reader from your stream, like so:

Reader reader = new InputStreamReader(is);
BufferedReader br = new BufferedReader(reader);

Preferrably, you also provide a Charset or character encoding name to the StreamReader constructor. Since a stream just provides bytes, converting these to text means the encoding must be known. If you don't specify it, the system default is assumed.


InputStream is;
InputStreamReader r = new InputStreamReader(is);
BufferedReader br = new BufferedReader(r);

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to inputstream

Convert InputStream to JSONObject How to read all of Inputstream in Server Socket JAVA Java - How Can I Write My ArrayList to a file, and Read (load) that file to the original ArrayList? How to create streams from string in Node.Js? How can I read a text file in Android? Can you explain the HttpURLConnection connection process? Open URL in Java to get the content How to convert byte[] to InputStream? Read input stream twice AmazonS3 putObject with InputStream length example

Examples related to readline

Read line with Scanner Python Serial: How to use the read or readline function to read more than 1 character at a time Getting rid of \n when using .readlines() How to use readline() method in Java? Convert InputStream to BufferedReader Why do I get the "Unhandled exception type IOException"? Best method for reading newline delimited files and discarding the newlines?

Examples related to bufferedreader

Java using scanner enter key pressed How to read until end of file (EOF) using BufferedReader in Java? How to use BufferedReader in Java Using BufferedReader.readLine() in a while loop properly Convert InputStream to BufferedReader Android Reading from an Input stream efficiently Scanner vs. BufferedReader Do I need to close() both FileReader and BufferedReader?