Java Advanced-io Stream (1)

Source: Internet
Author: User

http://download.csdn.net/detail/qq285016127/7963747

Java operation of the file API is generally divided into a byte stream of characters flow its read-write API framework for the file is also extended through this idea. In addition, the flow flow is divided into source and host streams. (The body of the stream is determined by flow direction, such as input inputstream/output stream outputstream)


<1> from the whole framework, the main points of knowledge of Io flow are divided into:

1. Byte stream (input fileinputstream/output FileOutputStream)

1) The construction of the byte stream is usually through the file class, or the absolute path of the file

2) The read of the file requires the FileInputStream class, its read () provides 3 methods that require us to provide the most basic byte byte array, the array as the buffered stream data by default, When there is no parameter, the system defaults to an array of length 1. Each time you finish reading, you put the data in the buffer.

3)FileInputStream provides a available () object method that returns the length of the data that is not blocked. In the case of small data, we can declare the length of the buffer as a length. read into memory once;

4) The writing of the file requires the FileOutputStream class, and its write () also provides the above three types of methods.

The sample code is as follows:

/** * Byte Stream Simple application * * @author Lean  @date: 2014-9-22   */public class Outputstreamsample {public static void main (string[] args) {String FilePath = "C:/users/administrator/desktop/aa.txt"; String Outfilepath = "C:/users/administrator/desktop/bb.txt"; InputStream fis=null;outputstream fos=null;int readNum= 0;//must be 2 of the n-th party, because the data may exist character byte[] buff=new byte[2];try {fis=new fileinputstream (filePath);//constructed by File,file full path or file descriptor If there is no file, the Fos=new FileOutputStream (outfilepath) is created automatically, while ((Readnum= (Fis.read (Buff)))!=-1) {//writes the file according to the offset fos.write ( Buff,0,readnum);}} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} Finally{try {if (fos!=null) {fos.close ();} if (fis!=null) {fis.close ();}} catch (IOException e) {e.printstacktrace ();}}}}

2. Character Stream , also known as a conversion stream (Inputstreamreader/outputstreamwriter)

1) character conversion stream, following the thought above, also provides a read operation, except that the definition of the buffer array is char[];

2) The character conversion stream is a byte-stream adorner, which integrates the advantages of the byte stream and is highly efficient. The code is as follows:

/** * Character Stream Simple application * * @author Lean  @date: 2014-9-22   */public class Characteriosample {public static void main (string[] A RGS) {final string filePath = "C:/users/administrator/desktop/aa.txt"; final string outfilepath = "c:/users/ Administrator/desktop/bb.txt "; Writer Writer=null; Reader reader=null;char[] buff=new char[512];int readnum=0;try {reader=new inputstreamreader (new FileInputStream ( FilePath)); Writer=new OutputStreamWriter (New FileOutputStream (Outfilepath)); while (Readnum=reader.read (buff))!=- 1) {writer.write (buff,0,readnum);}} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} Finally{try {if (writer!=null) {writer.close ();} if (reader!=null) {reader.close ();}} catch (IOException e) {e.printstacktrace ();}}}}

3. Buffer Stream (Bufferedreader/bufferedwriter)

1) the buffer stream is the adorner of the character conversion stream , which integrates the advantages of the character conversion stream , has a higher efficiency, and provides a way to read the whole line and write the whole line.

2) Buffer stream, as the name implies is built-in buffer data, we do not need to write buffer data (char[]/byte[]);

/** * Built-in buffer flow simple application *  * @author Lean @date: 2014-9-22 */public class Bufferediosample {public static void main (string[] Arg s) {final string filePath = "C:/users/administrator/desktop/aa.txt"; final string outfilepath = "c:/users/administrator/ Desktop/bb.txt "; BufferedWriter writer = null; BufferedReader reader = null; String appendstr =null;try {reader = new BufferedReader (new InputStreamReader (New FileInputStream (FilePath))); writer = New BufferedWriter (New OutputStreamWriter (New FileOutputStream (Outfilepath)); while (appendstr = Reader.readline ()) ! = null) {System.out.println (APPENDSTR); Writer.write (APPENDSTR);} Writer.flush ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} Finally{try {if (writer!=null) {writer.close ();} if (reader!=null) {reader.close ();}} catch (IOException e) {e.printstacktrace ();}}}}

4. Object Flow (Objectoutputstream/objectinputstream)

1) The stream can save the data to a file, the saved object needs to implement the serializable interface, and implement the sequence code (modify the class is useful)

2) If the saved object property is also an object, the above interface must also be implemented. Implement this interface to support persistence/network transport

3) The saved object file is garbled and cannot be opened directly. But if you want to be more secure, you must implement another interface externalizable implement your own save policy.

/** * * * @author Lean @date: 2014-9-22 */public class Objectiosample {public static void main (string[] args) {Saveobjec T (); ReadObject ();} public static void Saveobject () {String filepath= "c:\\users\\administrator\\desktop\\cc.txt"; ObjectOutputStream oos= null;try {oos=new ObjectOutputStream (new FileOutputStream (FilePath)); Oos.writeobject (New Student (1, "Lean",); o Os.writeobject (New Student (2, "Lucy"), Oos.writeobject (New Student (3, "Lout", 85)); catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} Finally{if (oos!=null) {try {oos.close ();} catch (IOException e) {e.printstacktrace ();}}}} public static void ReadObject () {String filepath= "c:\\users\\administrator\\desktop\\cc.txt"; ObjectInputStream ios= null;try {ios=new ObjectInputStream (new FileInputStream (FilePath)), Object object=null;for (int i = 0; i < 3; i++) {Syst  Em.out.println (Ios.readobject ());}} catch (ClassNotFoundException e) {e.printstacktrace ();} catch (FilenotfoundexcePtion e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} Finally{if (ios!=null) {try {ios.close ();} catch (IOException e) {e.printstacktrace ();}}}} }

/** * @author Lean  @date: 2014-9-22   */public class Student implements serializable{private int id;private String n ame;private int score;public Student (int id, String name, int score) {this.id = Id;this.name = Name;this.score = score;} @Overridepublic String toString () {return "Student [id=" + ID + ", name=" + name + ", score=" + score+ "]";}}

(PS: Another chapter in the High-level section is being summarized ...)


Java Advanced-io Stream (1)

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.