Java - Converting a byte-based stream to a character-based stream

Introduction

If you have an InputStream object, you can get a Reader object by wrapping it inside an InputStreamReader object

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class Main {
  public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("yourFile");
    Reader reader = new InputStreamReader(is);

  }
}

To construct a BufferedReader object from an InputStream object:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
  public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("yourFile");
    BufferedReader br = new BufferedReader(new InputStreamReader(is));

  }
}

You can construct a Writer object from an OutputStream object:

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class Main {
  public static void main(String[] args) throws Exception {
    OutputStream os = new FileOutputStream("yourFile");
    Writer writer = new OutputStreamWriter(os);
  }
}

Related Topic