FileReader
Role: for reading character streams
Construction method:
FileReader(File file)
Create a new
FileReader
, given
File
read.
FileReader(String fileName)
Creates a new
FileReader
, name of the file to be read given.
Common methods:
1)int read();
2)int read(char[] cbuf)
3)int read(char[] cbuf,int off,int len);
4) int available();
5)close();
Code
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class TestFileReader { public static void main(String[] args) throws IOException { FileReader reader=null; //(1)搭桥 reader=new FileReader("F://test.txt"); @ (2) reads // int reader.Read B = (); // int type data to the read character @ of System.out.print ((char) B); // int B = 0; // for each read character data storing integer value // the while (! (B = reader.Read ()) = -. 1) { // of System.out.print (( char) B); // } int len = 0; // stored character number read number char [] = CBUF new new char [1024 ]; while((len=reader.read(cbuf))!=-1) { System.out.print(new String(cbuf, 0, len)); } @ (3) off reader.Close (); } }
FileWriter
Role: for writing character streams
Construction method:
FileWriter(File file)
A File object to construct a FileWriter object. Given a file name with a Boolean value indicating whether additional data is written.
FileWriter(File file, boolean append)
A File object to construct a FileWriter object
FileWriter(String fileName)
Constructs a FileWriter object given a file name.
FileWriter(String fileName, boolean append)
Constructs a FileWriter object, is given a file name with a Boolean value indicating whether the data additional writing.
Common methods:
1)void write(int c);
2)void write(char[] cbuf);
3)abstract void write(char[] cbuf,int off,int len);
4)void write(String str);
5)abstract void flush();
6)void close();
Code
import java.io.FileWriter; import java.io.IOException; public class TestFileWriter { public static void main(String[] args) { //创建对象 FileWriter writer=null; try { Writer = new new FileWriter ( "F: //dest.txt" ); // write data writer.Write ( "the Hello World!"); // write data to the buffer writer.flush (); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { if(writer!=null) { //关闭 writer.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }