字符流
字符流的两个抽象父类:字符输出流:Writer 和字符输入流:Reader
1.字符输出流:Writer
使用字符输出流写入数据:使用 Writer 它的子类 OutputStreamWriter 类
OutputStreamWriter 类的成员方法
public void write(int c):写一个字符
public void write(char[] cbuf):写一个字符数组
public void write(char[] cbuf,int off,int len):写一个字符数组的一部分
public void write(String str):写一个字符串
public void write(String str,int off,int len):写一个字符串的一部分
flush():刷新缓冲区
close():关闭流对象
close() 和 flush() 的区别
close() 关闭流对象,但是先刷新一次缓冲区。关闭之后,流对象不可以继续再使用了。
flush() 仅仅刷新缓冲区,刷新之后,流对象还可以继续使用。
2.字符输入流:Reader
使用字符输入流读取数据:使用 Reader它的子类 InputStreamReader 类
InputStreamReader 类的构造方法
public InputStreamReader(InputStream in):用默认的编码读取数据
public InputStreamReader(InputStream in,String charsetName):用指定的编码读取数据
InputStreamReader 类的成员方法
public int read():一次读取一个字符
public int read(char[] cbuf):一次读取一个字符数组
close():关闭流对象
public class WriterTest {
public static void main(String[] args) throws IOException {
File f2=new File("D:/b.txt");
if (!f2.exists()){
f2.createNewFile();
}
Writer out=new FileWriter(f2);
out.write(97);
out.write('中');
char[] ch={'河','南','南','阳','农','业'};
out.write(ch,0,2);
out.write("移动2103班");
out.write("abcdef",0,3);
out.flush();
out.close();
}
}