Java-IO streams, Java IO stream characters

Source: Internet
Author: User

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 specifiedcharsetRead bytes and decode them into characters.

OutputStreamWriter is a bridge between the bytes stream and the byte stream: You can use the specifiedcharsetThe 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 ();}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.