例子
以下示例显示java.io.Writer.write()方法的用法。
package com.jc2182;
import java.io.*;
public class WriterDemo {
public static void main(String[] args) {
char[] c1 = {'h', 'e', 'l', 'l', 'o'};
char[] c2 = {'w', 'o', 'r', 'l', 'd'};
// create a new writer
Writer writer = new PrintWriter(System.out);
try {
// write a char array
writer.write(c1);
// flush the writer
writer.flush();
// write a new char array
writer.write(c2);
// flush the stream again
writer.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行以上程序,这将产生以下结果-
helloworld