IO stream for Java

Source: Internet
Author: User

1. Overview
Java IO through the java.io package below the interface and class to achieve, under the Java.io package includes input, output two IO streams, each input and output stream can be divided into two categories of byte stream and character stream. where the byte stream handles the input-output operation in bytes, where the character stream handles the input-output operation in characters.

The IO stream of Java is divided into the underlying node stream and the upper processing stream, where the node stream is used to directly correlate with the underlying physical node storage point-the way in which different physical nodes acquire the node stream may differ, but the program can wrap different physical nodes into a unified processing stream, allowing the program to use uniform input, Output code to read the resources of different physical storage nodes.

2. Byte stream
2.1InputStream Abstract class
InputStream is the abstract base class for input streams in all byte streams.
2.1.1FileInputStream class

Import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.ioexception;public class fileinputstreamtest {public static void main (string[] args) {try {//Create a byte input stream fileinputstream fis=new FileInputStream (" Src/com/t/lavor_zl.txt "); byte[] buf=new byte[1024];//Create a byte array of length 1024 int hasread=0;//to hold the number of bytes actually read/*read () The method also has two overloads: Read () reads a byte, returns the read Byte,                        *read (char[] buf,int off,int len) reads Len bytes from the off position                        * and stores it in buf, returning the number of bytes actually read                        */while ((Hasread=fis.read (BUF)) >0) {System.out.println (new String (Buf,0,hasread));} Fis.close ();//close input stream} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {//TODO Auto-generated catch Blocke.printstacktrace ();}}}

PS: Chinese characters accounted for 2 characters, if only read a character of Chinese characters, will appear garbled.
2.2OutputStream Abstract class
OutputStream is the abstract base class for output streams in all byte streams.
2.2.1FileOutputStream class

Import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;public class fileoutputstreamtest {public static void main (string[] args) {try {fileoutputstream fos=new fileoutputstream ("src/com/t /lavor.txt "); byte[] Buf=new byte[1024]; String str= "Lavor_zl";//convert string to character array///                                          *write () method There are also two overloads: writer (int b) outputs the specified bytes to the input stream;                        * writer (byte[] buf) outputs the character array to the output stream                        */fos.write (buf, 0, Str.length ()), Fos.close ();//close output stream} catch ( FileNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Auto-gene Rated catch Blocke.printstacktrace ();}}}

3. Character Stream
3.1Reader Abstract class
Reader is the abstract base class for the input stream in all character streams.
3.1.1FileReader class

Import Java.io.filenotfoundexception;import Java.io.filereader;import Java.io.ioexception;public class filereadertest {public static void main (string[] args) {try {//create an input stream FileReader fr=new filereader ("Src/com/t/lavor_ Zl.txt "); char[] buf=new char[1024];int hasread=0;/*read () method also has two overloads: Read () reads a character, returns the read character; *read (char[] Buf,int off,int Len) reads the Len character * from the off position and stores it in buf, returning the actual number of characters read */while ((Hasread=fr.read (BUF)) >0) {System.out.println (New String ( Buf,0,hasread));}} catch (FileNotFoundException e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//Todo Au To-generated catch Blocke.printstacktrace ();}}}

3.2Writer Abstract class
Writer is the abstract base class for the output stream in all character streams.
3.2.1FileWriter class

Import Java.io.filewriter;import Java.io.ioexception;public class Filewritertest {public static void main (string[] args {try {FileWriter fw=new FileWriter ("Src/com/t/lavot.txt"); char[] buf=new char[]{' l ', ' a ', ' V ', ' o ', ' R '};fw.write (127) ;//input specified character to output stream, parameter is int type fw.write (BUF);//input specified character array to output stream Fw.write (buf, 0, 3);//outputs 3 characters from 0 position to output stream fw.write from the specified character array ("Lavor_ ZL ");//Enter the specified string into the output stream Fw.write (" Lavor_zl ", 0, 3);//outputs from the specified string 3 characters starting from 0 position to the output stream} catch (IOException e) {//TODO auto-generated Catch Blocke.printstacktrace ();}}}

4.IO Flow System

category byte input stream byte output stream character input stream character output stream
Abstract base class InputStream
OutputStream Reader Writer
accessing files FileInputStream FileOutputStream FileReader FileWriter
accessing arrays Bytearrayinputstream Bytearrayoutputstream CharArrayReader Chararraywriter
Access pipeline PipedInputStream PipedOutputStream Pipedreader PipedWriter
accessing strings StringReader StringWriter
Buffered streams Bufferedinputstream Bufferedoutputstream BufferedReader BufferedWriter
Convert stream InputStreamReader OutputStreamWriter
Object Flow ObjectInputStream ObjectOutputStream
Abstract base class FilterInputStream Filteroutputstream FilterReader Filterwriter
Print Flow PrintStream PrintWriter
Pushing back the input stream Pushbackinputstream Pushbackreader
Special flow DataInputStream DataOutputStream
Red represents an abstract base class and cannot be created directly; blue represents a byte stream and must be directly associated with the specified physical node


IO stream for Java

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.