FileReader是用于读取字符流的类,它继承于InputStreamReader,要读取原始字节流,考虑使用FileInputStream;FileWriter是用于写入字符流的类,继承于OutputStreamWriter,要写入原始字节流,考虑使用FileOutputStream。
FileReader和FileWriter的示例代码:
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的数据会自动由字节转换为字符。 FileWriter out1 = new FileWriter(file); // 写入10个汉字 out1.write("字节流转为字符流示例"); // 向“文件中”写入"0123456789"+换行符 out1.write("0123456789\n"); out1.close(); } catch(IOException e) { e.printStackTrace(); } } /** * InputStreamReader 演示程序 */ private static void testRead() { try { // 方法1:新建FileInputStream对象 // 新建文件“file.txt”对应File对象 File file = new File(FileName); FileReader in1 = new FileReader(file); // 测试read(),从中读取一个字符 char c1 = (char)in1.read(); System.out.println("c1="+c1); // 测试skip(long byteCount),跳过4个字符 in1.skip(6); // 测试read(char[] cbuf, int off, int len) char[] buf = new char[10]; in1.read(buf, 0, buf.length); System.out.println("buf="+(new String(buf))); in1.close(); } catch(IOException e) { e.printStackTrace(); } } }
运行结果:
c1=字
buf=流示例0123456
基于JDK8的FileReader的源代码:
public class FileReader extends InputStreamReader { /** * Creates a new <tt>FileReader</tt>, given the name of the * file to read from. * * @param fileName the name of the file to read from * @exception FileNotFoundException if the named file does not exist, * is a directory rather than a regular file, * or for some other reason cannot be opened for * reading. */ public FileReader(String fileName) throws FileNotFoundException { super(new FileInputStream(fileName)); } /** * Creates a new <tt>FileReader</tt>, given the <tt>File</tt> * to read from. * * @param file the <tt>File</tt> to read from * @exception FileNotFoundException if the file does not exist, * is a directory rather than a regular file, * or for some other reason cannot be opened for * reading. */ public FileReader(File file) throws FileNotFoundException { super(new FileInputStream(file)); } /** * Creates a new <tt>FileReader</tt>, given the * <tt>FileDescriptor</tt> to read from. * * @param fd the FileDescriptor to read from */ public FileReader(FileDescriptor fd) { super(new FileInputStream(fd)); } }
由源码分析得,FileReader继承于InputStreamReader,因此是使用的父类的操作。
基于JDK8 的FileWriter的源码分析:
public class FileWriter extends OutputStreamWriter { /** * Constructs a FileWriter object given a file name. * * @param fileName String The system-dependent filename. * @throws IOException if the named file exists but is a directory rather * than a regular file, does not exist but cannot be * created, or cannot be opened for any other reason */ public FileWriter(String fileName) throws IOException { super(new FileOutputStream(fileName)); } /** * Constructs a FileWriter object given a file name with a boolean * indicating whether or not to append the data written. * * @param fileName String The system-dependent filename. * @param append boolean if <code>true</code>, then data will be written * to the end of the file rather than the beginning. * @throws IOException if the named file exists but is a directory rather * than a regular file, does not exist but cannot be * created, or cannot be opened for any other reason */ public FileWriter(String fileName, boolean append) throws IOException { super(new FileOutputStream(fileName, append)); } /** * Constructs a FileWriter object given a File object. * * @param file a File object to write to. * @throws IOException if the file exists but is a directory rather than * a regular file, does not exist but cannot be created, * or cannot be opened for any other reason */ public FileWriter(File file) throws IOException { super(new FileOutputStream(file)); } /** * Constructs a FileWriter object given a File object. If the second * argument is <code>true</code>, then bytes will be written to the end * of the file rather than the beginning. * * @param file a File object to write to * @param append if <code>true</code>, then bytes will be written * to the end of the file rather than the beginning * @throws IOException if the file exists but is a directory rather than * a regular file, does not exist but cannot be created, * or cannot be opened for any other reason * @since 1.4 */ public FileWriter(File file, boolean append) throws IOException { super(new FileOutputStream(file, append)); } /** * Constructs a FileWriter object associated with a file descriptor. * * @param fd FileDescriptor object to write to. */ public FileWriter(FileDescriptor fd) { super(new FileOutputStream(fd)); } }