Java Java.io.OutputStreamWriter.write() 方法

  • 描述

    java.io.OutputStreamWriter.write(int c)方法写入一个字符。
  • 声明

    以下是java.io.OutputStreamWriter.write()方法的声明。
     public void write(int c)
  • 参数

    c -int指定要写入的字符。
  • 返回值

    此方法不返回值。
  • 异常

    IOException如果发生I / O错误。
  • 例子

    以下示例显示java.io.OutputStreamWriter.write()方法的用法。
     
    package com.jc2182;
    import java.io.*;
    
    public class OutputStreamWriterDemo {
       public static void main(String[] args) {
          try {
             // create a new OutputStreamWriter
             OutputStream os = new FileOutputStream("test.txt");
             OutputStreamWriter writer = new OutputStreamWriter(os);
    
             // create a new FileInputStream to read what we write
             FileInputStream in = new FileInputStream("test.txt");
    
             // write something in the file
             writer.write(70);
             writer.write(71);
             writer.write(72);
    
             // flush the stream
             writer.flush();
    
             // read what we write
             for (int i = 0; i < 3; i++) {
                System.out.print("" + (char) in.read());
             }
    
          } catch (Exception ex) {
             ex.printStackTrace();
          }
       }
    }
    
    让我们编译并运行以上程序,这将产生以下结果-
     FGH