java io进修(二十一) FileReader和FileWriter
FileReader和FileWriter源码阐明
1. FileReader 源码(基于jdk1.7.40)
package java.io; public class FileReader extends InputStreamReader { public FileReader(String fileName) throws FileNotFoundException { super(new FileInputStream(fil java io系列21之 InputStreamReader和OutputStreamWritereName)); } public FileReader(File file) throws FileNotFoundException { super(new FileInputStream(file)); } public FileReader(FileDescriptor fd) { super(new FileInputStream(fd)); } }
从中,我们可以看出FileReader是基于InputStreamReader实现的。
2. FileWriter 源码(基于jdk1.7.40)
package java.io; public class FileWriter extends OutputStreamWriter { public FileWriter(String fileName) throws IOException { super(new FileOutputStream(fileName)); } public FileWriter(String fileName, boolean append) throws IOException { super(new FileOutputStream(fileName, append)); } public FileWriter(File file) throws IOException { super(new FileOutputStream(file)); } public FileWriter(File file, boolean append) throws IOException { super(new FileOutputStream(file, append)); } public FileWriter(FileDescriptor fd) { super(new FileOutputStream(fd)); } }
从中,我们可以看出FileWriter是基于OutputStreamWriter实现的。
示例措施
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;;
import java.io.FileReader;
import java.io.IOException;/**
* FileReader 和 FileWriter 测试措施
*
* @author skywang
*/
public class FileReaderWriterTest {private static final String FileName = "file.txt";
private static final String CharsetName = "utf-8";public static void main(String[] args) {
testWrite();
testRead();
}/**
* OutputStreamWriter 演示函数
*
*/
private static void testWrite() {
try {
// 建设文件“file.txt”对应File工具
File file = new File(FileName);
// 建设FileOutputStream对应FileWriter:将字节约转换为字符流,即写入out1的数据会自动由字节转换为字符。
// 查察本栏目c1=字
buf=流示例0123456
来历:http://www.cnblogs.com/skywang12345/p/io_22.html