Java read-Write text file

Source: Internet
Author: User

1 character input (FileReader, char)

Import Java.io.ioexception;import Java.io.filereader;public class Ep10_1 {public    static void Main (string[] args) Throws ioexception{        //Reference object B        filereader b = new FileReader ("/tmp/ep10_1.txt");        The reader space that defines the text store        char[] A = new char[1000];        Reads the contents of object B into a, returning the number of characters        int num = B.read (a);        Converts the character a to str output        string str = new string (a,0,num);        SYSTEM.OUT.PRINTLN ("file reads: \ n" +str); B.close ();} }

2 character output (FileWrite char)

Import Java.io.filewriter;import Java.io.ioexception;public class Ep10_3 {public    static void Main (string[] args) { c1/>try{            FileWriter A = new FileWriter ("/tmp/wt.txt");            for (int i=32;i<126;i++) {                //char type write                a.write (i);            }            A.close ();        } catch (IOException e) {}}    }

3 character input and output (Bufferedreader,bufferedwriter,char )

Import Java.io.*;import Java.nio.buffer;public class Ep10_4 {public    static void Main (string[] args) {        String str = new String ();        try{            //bufferedreader refers to a type of string, which means that BufferedReader converts the text of the FileReader character to string        bufferedreader in = new BufferedReader (New FileReader ("/tmp/ep10_1.txt"));        BufferedWriter out = new BufferedWriter (New FileWriter ("/tmp/ep10_4.txt"));        while ((Str=in.readline ())!=null) {            System.out.println (str);            Out.write (str);            Out.newline ();        }        Out.flush ();        In.close ();        Out.close ();    } catch (IOException e) {            System.out.println ("Error contents:" +e);}}}    

4 bytes of input

ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStream; Public classEp10_5 { Public Static voidMain (string[] args) {Try {            byte[] bt =New byte[1000]; FileInputStream ins=NewFileInputStream ("/tmp/ep10_1.txt"); intnum =Ins.read (BT); String Str=NewString (bt,0, num); System.out.println ("Contents:\n" +str); }Catch(IOException e) {System.out.println ("Error:\n" +e); }    }}

Java read-Write text file

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.