Java BufferedWriter create from OutputStream using "US-ASCII" encoding

Description

Java BufferedWriter create from OutputStream using "US-ASCII" encoding

import java.io.BufferedWriter;
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("Main.java");
    Writer writer = new OutputStreamWriter(os, "US-ASCII");

    try (BufferedWriter bw = new BufferedWriter(writer)) {
      bw.append("demo2s.com");
      bw.newLine();/* w  w w .j  a  va  2  s.com*/
      bw.flush();
      System.out.println("saved");
    } catch (Exception e2) {
      e2.printStackTrace();
    }
  }
}



PreviousNext

Related