Java study file read/write

Source: Internet
Author: User

File read/write

If you write a large amount of data in your code, it will increase the redundancy of the Code, by reading the file, you can streamline the code, easy to modify the data and code maintenance
Classification of IO streams: byte stream and character stream

Character Stream
    • Character output stream: The abstract base class Java.io.Writer that writes a text file. Writing method Write, many overloaded forms, an array of characters, a single character, a string, a part of a string, a part of a string, a buffer of flush data flow, close object.
    • Character input stream: The abstract base class Java.io.Reader that reads the text file. Read method reads, many overloaded forms, reading a single character, an array of characters, and part of a character array. Close closes the stream object.

      BYTE stream
    • BYTE output stream: Writes arbitrary file, abstract base class Java.io.OutputStream. Write a method written, many overloaded forms, write a single byte, character array, part of a byte array, close the stream object.
    • BYTE input stream: reads arbitrary file, abstract base class Java.io.InputStream. Read the method reads, many overloaded forms, read a single byte, character array, part of a byte array, close the stream object.
      The nomenclature for all classes of IO Six: The suffix is the parent class name, the preceding is the file name that can be manipulated, the flow name.
      FileInputStream FileReader ObjectInputStream ObjectOutputStream
#javapackage study1;import org.testng.annotations.test;import java.io.*;p ublic class Iodemo {@org. junit.test public void Test () throws IOException {//If the file does not exist, it is automatically created FileWriter fw = new FileWriter (new file ("/users/chenshanju        /desktop/data/a.txt "));        Fw.write ("Hello world\n");        Fw.write ("123\n");         /** * After writing the file, no other action is taken, the content written will not be saved * After writing to the file, the close stream will automatically save the file.        * More write data is, after using write writing, use flush to save the written content, reduce the pressure on the system */Fw.flush ();        Fw.write (123);//The ASCII code value Fw.flush () is written here;        Fw.close ();//Release stream resource} @org. junit.test public void Test2 () {FileWriter fw= null;        try{FW = new FileWriter (New File ("/users/chenshanju/desktop/data/a.txt"));        }catch (IOException e) {e.printstacktrace (); }finally{try{fw.close ();//to release a stream resource here, you must first initialize}catch (IOException e) {e.            Printstacktrace ();   }}} @org. junit.test public void Append () throws ioexception{//append content, not overwrite FileWriter fw = new FileWriter ("/users/chensh        Anju/desktop/data/a.txt "), true);        Fw.write ("Hello python\n");        Fw.flush ();    Fw.close ();        } @org. junit.test public void Test_read () throws ioexception{//Read file FileWriter fw = null;        FW = new FileWriter (New File ("/users/chenshanju/desktop/data/a.txt"));        Fw.write ("ABCD");        Fw.flush ();        Fw.close ();        FileReader FR = null;        FR = new FileReader (New File ("/users/chenshanju/desktop/data/a.txt"));        System.out.println (Fr.read ());//The ASCII value of the output character, read 1 times System.out.println ((char) fr.read ()) per execution;        System.out.println (Fr.read ());        System.out.println (Fr.read ());        System.out.println (Fr.read ());    System.out.println (Fr.read ());//no content, output-1 fr.close ();     } @org. junit.test public void Test_read2 () throws ioexception{//Read single character FileWriter FW = null;   FW = new FileWriter (New File ("/users/chenshanju/desktop/data/a.txt"));        Fw.write ("ABCD");        Fw.flush ();        Fw.close ();        FileReader FR = null;        FR = new FileReader (New File ("/users/chenshanju/desktop/data/a.txt"));        int i=0; while ((i= fr.read ())!=-1) {/** * Gets the ASCII value of the character and is strongly converted to the character */System.out according to the ASCII code value.        println ((char) i);    } fr.close (); } @org. junit.test public void Test_read3 () throws ioexception{//read character array, read 2 characters at a time FileWriter fw = new F        Ilewriter (New File ("Data/b.txt"));        Fw.write ("Java");        char [] cha = {' A ', ' B ', ' C '};        Fw.write (CHA);        Fw.close ();        FileReader FR = new FileReader (New File ("Data/b.txt"));        int i = 0;            char [] ch = new char[2];//reads 2 characters at a Time ((I=fr.read (CH))!=-1) {System.out.print (i+ "\ t");            SYSTEM.OUT.PRINTLN (CH); /** * SYSTEM.OUT.PRINTLN (i+ "\ T" +new String (CH));            * used in conjunction with int type, without new string, will return hash code [[[email protected] * Plus new string and the above 2 line output results consistent * 2     JA * 2 VA * 2 AB * 1 CB last 1 times only 1 characters are loaded, only a is replaced with c,b not replaced by */}        } @org. Junit.test//single-character write public void Test_cp () throws ioexception{FileReader fr = null;        FileWriter FW = NULL;        FR = new FileReader (New File ("Data/b.txt"));        FW = new FileWriter (New File ("Data/b1.txt"));        int i = 0;        while ((I=fr.read ())!=-1) {fw.write ((char) i);        } fw.close ();    Fr.close ();        } @org. junit.test//character array is written to public void Test_cp1 () throws ioexception{FileReader fr = null;        FileWriter FW = NULL;        FR = new FileReader (New File ("Data/b.txt"));        FW = new FileWriter (New File ("Data/b2.txt"));        char [] ch = new CHAR[2];        int i = 0;        while ((I=fr.read (CH))!=-1) {fw.write (ch,0,i); } fw.clOSE ();    Fr.close ();        } @org. junit.test public void Test_out () throws ioexception{//bytes output stream fileoutputstream out = null;        out = new FileOutputStream (New File ("Data/c.txt"));        Out.write ("Abcdefgh". GetBytes ());    Out.close ();        } @org. junit.test public void test_in () throws ioexception{//byte input stream fileinputstream in = null;        in = new FileInputStream (New File ("Data/c.txt"));        int i = 0;        while ((I=in.read ())!=-1) {System.out.println ((char) i);    } in.close ();  } @org. junit.test public void Test_in_char () throws ioexception{fileinputstream in = new FileInputStream (new        File ("Data/c.txt"));        int i = 0;        byte [] bt = new BYTE[10];            while ((I=in.read (BT))!=-1) {System.out.print (i+ "\ T" +new String (BT));     /** * SYSTEM.OUT.PRINTLN (BT); return hash code [[email protected] * length less than 10 bits, will be filled with empty */} } @org. JUnit.Test public void Test_io () throws ioexception{//Save the source of a Web page as a file FileInputStream in = new FileInputStream (NE        W File ("data/d.html"));        FileOutputStream out = new FileOutputStream (New File ("data/d1.html"));        int i = 0;        byte [] bt = new byte[1024];        while ((I=in.read (BT))!=-1) {out.write (bt,0,i);        } out.close ();        In.close ();    SYSTEM.OUT.PRINTLN ("Success"); }}

Java study file read/write

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.