Java-IO streams, Java IO stream characters
Java text (char) is a 16-bit unsigned integer and unicode (dubyte encoding) of characters ).
The file is a data sequence of byte.
A text file is a storage result of serialized text (char) sequences into bytes according to a certain encoding scheme (uft-8, utf-16be, gbk.
- Pipeline stream (Reader, Writer) --> operates on text and text files
1. Processing of characters, processing one character at a time
2. The bottom layer of the character is still the basic byte sequence
3. Basic Implementation of the streaming:
InputStreamReader is a bridge between byte streams: it uses the specifiedcharset
Read bytes and decode them into characters.
OutputStreamWriter is a bridge between the bytes stream and the byte stream: You can use the specifiedcharset
The characters to be written into the stream are encoded into bytes.
public class ISReaderAndOSWriter { public static void main(String[] args) throws IOException { FileInputStream in = new FileInputStream("F:\\javaio\\java.txt"); FileOutputStream out = new FileOutputStream("F:\\javaio\\java1.txt"); InputStreamReader isr = new InputStreamReader(in, "gbk"); OutputStreamWriter osw = new OutputStreamWriter(out, "gbk"); int c;// while ((c = isr.read()) != -1) {// System.out.print((char)c);// } char[] buf = new char[8 * 1024]; while ((c = isr.read(buf, 0, buf.length)) != -1) { String s = new String(buf, 0, c); //System.out.println(s); osw.write(s); } isr.close(); osw.close(); }}
4. file read/write streams, FileReader, and FileWriter are convenient classes for reading character files and writing character files.
Public class FileReaderAndFileWriter {public static void main (String [] args) throws Exception {FileReader fr = new FileReader ("F: \ javaio \ javautf.txt "); fileWriter fw = new FileWriter ("F: \ javaio \ javautf1.txt"); // FileWriter fw = new FileWriter ("F: \ javaio \ javautf1.txt", true ); // append the content char [] buf = new char [1*1024]; int c; while (c = fr. read (buf, 0, buf. length ))! =-1) {System. out. println (c); fw. write (buf, 0, c); fw. flush ();} fr. close (); fw. close ();}}
5. filters of the upstream stream
BufferedReader reads a row at a time
BufferedWriter/PrintWriter, write a row at a time
Public class BufRAndBufWOrPrintW {public static void main (String [] args) throws Exception {BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream ("F: \ javaio \ javautf.txt "); // BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (" F: \ javaio \ javautf2.txt "))); printWriter pw = new PrintWriter ("F: \ javaio \ javautf3.txt"); String s; while (s = Br. readLine ())! = Null) {// read a row at a time. The line break cannot be recognized. // bw. write (s); // bw. newLine (); // write a line separator // bw. flush (); pw. println (s); // terminate the pw of the current row by writing the row separator string. flush ();} br. close (); // bw. close (); pw. close ();}}