use of five character streams (1) Coding problems
We have already said this question, and we will not repeat it too much.
Reference: Javase Getting Started learning 42: I/O stream of File transfer Basics (i)
(2) Understanding text and text files
The Java text (char) is a 16-bit unsigned integer, which is the Unicode encoding of the character (double-byte encoding), and the file is a byte byte byte ... of data
A text file is a stored result of a sequence of text (char) serialized into Byte according to an encoding scheme (UTF-8,UTF-16BE,GBK, etc.).
(3) Character stream (Reader Writer)----> operation is text text file
The processing of characters, one character at a time.
The bottom of the character is still the basic byte sequence.
(4) Basic realization of character stream 1) InputStreamReader class and OutputStreamWriter class
The InputStreamReader class is the completion of a byte stream resolved to a char stream, parsed according to the encoding.
methods of the InputStreamReader class:
The OutputStreamWriter class is provided with a char stream to a byte stream, which is processed by encoding.
methods of the OutputStreamWriter class:
Instance code:
Import java.io.*;p ublic class israndoswdemo{public static void Main (string[] args) throws IOException {FileInputStream in = new FileInputStream ("E:\\java\\javase\\io\\testutf8.txt"); InputStreamReader ISR = new InputStreamReader (in);// The default item encoding, when operating, is to write the format of the file itself //inputstreamreader ISR = new InputStreamReader (in, "GBK"); FileOutputStream out = new FileOutputStream ("E:\\java\\javase\\io\\testutf81.txt"); OutputStreamWriter OSW = new OutputStreamWriter (out);//int c;//while ((c = isr.read ())!=-1) {//system.out.print ((char) c);//}char[] buffer = new Char [8*1024];int b;/* * Bulk Read, put in the buffer of this character array, starting from the No. 0 position, put up buffer.length * Return is the number of characters read */while (b = Isr.read (buffer,0, buffer.length))!=-1) {string s = new String (buffer,0,b); System.out.print (s); Osw.write (buffer,0,b); Osw.flush (); Isr.close (); Osw.close ();}}
Operation Result:
2) FileReader class and FileWriter class
The FileReader class is a convenient class for reading character files.
FileWriter class a handy class for writing character files.
the methods in the FileReader class and the FileWriter class are derived from inherited methods and do not have their own unique methods.
Instance code:
Import java.io.*;p Ublic class Frandfwdemo {public static void main (string[] args) throws ioexception{ //using the default encoding format, The encoding format FileReader FR = new FileReader ("E:\\java\\javase\\io\\test.txt") cannot be specified here; FileWriter FW = new FileWriter ("E:\\java\\javase\\io\\test2.txt");//continue to append content//filewriter fw = new FileWriter ("e:\\ Java\\javase\\io\\test2.txt ", true); char[] buffer = new Char[2056];int C; while ((c = Fr.read (buffer,0,buffer.length))! = -1) {fw.write (buffer,0,c); Fw.flush ();} Fr.close (); Fw.close ();}}
Operation Result:
(5) filter for character Stream
BufferedReader class---->readline read one line at a time
BufferedReader class reads text from the character input stream, buffering individual characters, enabling efficient reading of characters, arrays, and rows. You can specify a slow
The size of the flush area, or the default size can be used. In most cases, the default value is large enough.
Bufferedwriter/printwriter class----> Write a line
BufferedWriter class writes text to the character output stream, buffering individual characters, enabling efficient reading of characters, arrays, and rows.
The PrintWriter class prints a formatted representation of the object to the text output stream.
Methods of the BufferedReader class:
methods of the BufferedWriter class:
methods of the PrintWriter class:
Instance:
Import java.io.*;p Ublic class Brandbworpwdemo {public static void main (string[] args) throws ioexception{//read and write to a file Buff Eredreader br = new BufferedReader (New InputStreamReader (Newfileinputstream ("E:\\java\\javase\\io\\test.txt"));// BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (New FileOutputStream ("E:\\java\\javase\\io\\test3.txt") ))); PrintWriter pw = new PrintWriter ("E:\\java\\javase\\io\\test4.txt");//printwriter pw1 = new PrintWriter (OutputStream, Boolean autoflush); String line, while (lines = Br.readline ())!=null) {System.out.println (line);//read one row at a time and do not recognize the newline//bw.write;// Write the newline action separately//bw.newline ();//newline Operation//bw.flush ();p w.println (line);p W.flush (); Br.close ();//bw.close ();p w.close ();}}
Operation Result:
Javase Getting Started learning 45: I/O stream of File transfer Basics (iv)