Java character stream

Source: Internet
Author: User

/* Character Stream Reader Writer--filewriter character stream buffer: improve flow operation efficiency BufferedReader BufferedWriter */package Pack;import Java.io.bufferedreader;import Java.io.bufferedwriter;import Java.io.filereader;import Java.io.FileWriter;import    Java.io.linenumberreader;public class Main {public static void sys (Object obj) {System.out.println (obj); } public static void Main (string[] args) throws exception{//method1 ();//filewriter//method2 ();//filewri ter//method3 ();//filereader//method4 ();//Copy File//method5 ();//bufferedwriter//method6 ();//fil EReader//method7 ();//Copy File Method8 ();//linenumberreader add serial number function} public static void Method1 () throws Exc        eption {FileWriter fw = new FileWriter ("E:\\1.txt"); Fw.write ("abc"); Writes a string to the stream Fw.flush (); Refreshes the data in the buffer in the Stream object Fw.close (); Off resource} public static void Method2 () throws Exception {FileWriter fw = new FileWriter ("E:\\1.txt", true);//No Cover the file Fw.write ("cc\r\nC ");        NewLine Fw.flush ();    Fw.close ();        } public static void Method3 () throws Exception {FileReader fr = new FileReader ("E:\\1.txt");        /*int ch = 0;        while ((Ch=fr.read ())!=-1) {sys (char) ch);        }*//*char[] buf = new CHAR[9];        int ch = fr.read (BUF);//writes the array, CH is the number of Read SYS (new String (BUF));//output array */char[] buf = new Char[3];        int num = 0;         while ((Num=fr.read (BUF))!=-1)//number is not 1 sys (new String (Buf,0,num));//from 0 to Num fr.close ();        }/* Copy file */public static void Method4 () throws Exception {/*filereader fr = new FileReader ("E:\\1.txt");        FileWriter FW = new FileWriter ("E:\\2.txt");        int ch = 0;        while ((Ch=fr.read ())!=-1) fw.write (CH);        Fr.close ();        Fw.close (); */FileReader FR = new FileReader ("E:\\1.txt");        FileWriter FW = new FileWriter ("E:\\2.txt");        char[] buf = new char[1024];        int len = 0; While((Len=fr.read (BUF))!=-1) Fw.write (Buf,0,len);        Fw.close ();    Fr.close (); The public static void Method5 () throws Exception {/* buffer is designed to increase efficiency by first having a stream */FileWriter FW = new FileWriter ("E:\        \1.txt ");        BufferedWriter BUFW = new BufferedWriter (FW);        Bufw.write ("KK");  Bufw.newline ();        Write line break bufw.write ("GG");        Bufw.flush (); Bufw.close ();//close buffer is the stream object that closed buffer function} public static void Method6 () throws Exception {FileReader fr = new FileR        Eader ("E:\\1.txt");        BufferedReader bufr = new BufferedReader (FR);        String line = null;        while ((Line=bufr.readline ())!=null)//Returns an empty representation read to the end of Sys (line);    Bufr.close (); }/* Copy file */public static void Method7 () throws Exception {BufferedReader bufr = new BufferedReader (New FileR        Eader ("E:\\1.txt"));        BufferedWriter BUFW = new BufferedWriter (New FileWriter ("E:\\2.txt"));        String line = null; while ((Line=bufr.readline ())! =NULL) Bufw.write (line);        Bufr.close ();    Bufw.close ();        } public static void Method8 () throws Exception {FileReader fr = new FileReader ("E:\\1.txt");        LineNumberReader LNR = new LineNumberReader (FR);        String line = null;        while ((Line=lnr.readline ())!=null) sys (lnr.getlinenumber () + ":" +line);    Lnr.close (); }}

Java character stream

Related Article

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.