FileReader and FileWriter Source analysis
1. FileReader source code (based on jdk1.7.40)
Package java.io;
public class FileReader extends InputStreamReader {public
filereader (String fileName) throws FileNotFoundException {
Super (new FileInputStream (fil Java IO Series 21 inputstreamreader and Outputstreamwriterename));
}
Public filereader (file file) throws FileNotFoundException {
super (new FileInputStream (file));
}
Public FileReader (FileDescriptor FD) {
super (new FileInputStream (FD));
}
From this, we can see that FileReader is based on inputstreamreader implementation.
2. FileWriter source code (based on 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));
}
From this, we can see that FileWriter is based on outputstreamwriter implementation.
Sample Programs
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 and FileWriter Test program * * @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 demo function * */private static void Testwrite () {try {
Create the file "file.txt" corresponding to the files object File File = new file (FileName);
Create FileOutputStream corresponds to FileWriter: Converts a stream of bytes into character streams, where data written to OUT1 is automatically converted to characters by Byte. See more highlights of this column: Http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/FileWriter out1 = new FileWriter
(file);
Writes 10 Kanji out1.write ("Byte flow is example of character stream");
Write to "0123456789" + newline character in "file" Out1.write ("0123456789\n");
Out1.close ();
catch (IOException e) {e.printstacktrace ();
}/** * InputStreamReader demo/private static void Testread () {try {
Method 1: New FileInputStream object//New file "file.txt" corresponds to the file object, File File = new (FileName);
FileReader in1 = new FileReader (file);
Test read (), which reads a char C1 = (char) in1.read ();
System.out.println ("c1=" +c1);
Test Skip (Long ByteCount), skipping 4 characters in1.skip (6);
Test 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 (); }
}
}
Run Result:
c1= Word
buf= Flow Example 0123456
Source: http://www.cnblogs.com/skywang12345/p/io_22.html