OutputStreamWriter

In this chapter you will learn:

  1. How to use OutputStreamWriter

Use OutputStreamWriter

An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset.

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
//from j a  v  a  2  s . com
public class Main {
  public static void main(String[] args) throws Exception {
    try {
      FileOutputStream fos = new FileOutputStream("test.txt");
      Writer out = new OutputStreamWriter(fos, "UTF8");
      out.write("java2s.com");
      out.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

Next chapter...

What you will learn in the next chapter:

  1. How to use RandomAccessFile
  2. How to create a RandomAccessFile in read mode
  3. How to seek position with RandomAccessFile
  4. How to get file length from RandomAccessFile
  5. How to read into a byte array
  6. Read a single byte
  7. Seek position in a RandomAccessFile
  8. How to write byte array into RandomAccessFile
  9. How to write a String to a RandomAccessFile