---------------------- android培训、java培训、期待与您交流! ----------------------
Reader与Writer类
Reader与Writer是所有字符流类的抽象基类,用于简化对字符串的输入输出编程,即用于读写文本数据。
例子:
TestCharStream.java
import java.io.*;
public class TestCharStream {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("b.txt");
fw.write("www.it315.com".toCharArray());
fw.close();
File f = new File("b.txt");
char[] cbuf = new char[1024];
FileReader fr = new FileReader(f);
int len = fr.read(cbuf);
System.out.println(new String(cbuf, 0, len));
fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
注意:在FileOutputStream中使用write(byte[] buf) 方法时,就算不使用flush()或者close()方法,也能将数据写入文件中,因为write(byte[] buf) 方法中本身有调用flush()方法;而在Writer中使用write(char[] cbuf) 方法时,如果不使用flush()或者close()方法,则该方法并没有把数据写入文件,因为此方法把数据写在缓冲区而没有调用flush()或者close()方法。
---------------------- android培训、java培训、期待与您交流! ----------------------详细请查看:http://edu.youkuaiyun.com/heima