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().

  • Cannot Add Library to Android Project in Eclipse
  • Cannot create files on Android with Xamarin
  • onPrepareOptionsMenu not getting called in Fragments
  • UI Testing tool android from ADT21
  • Ideas of Source of QualcommCameraHardware native_get_picture: MSM_CAM_IOCTL_GET_PICTURE Connection timed out error?
  • Fragment from View Pager hiding behind Tab Bar
  • 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!

  • How to create RecyclerView with multiple view type?
  • Garbage Collection causes : MediaPlayer finalized without being released
  • Android Hardware Acceleration - to use or not to use?
  • Threading Example in Android
  • ACCESS_FINE_LOCATION AndroidManifest Permissions Not Being Granted
  • Get milliseconds until midnight
  • 3 Solutions collect form web for “Convert InputStream to BufferedReader”

    BufferedReader can’t wrap an InputStream directly. It wraps another Reader. In this case you’d want to do something like:

    BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    

    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);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.