io-overview-Inputs and outputs

Source: Internet
Author: User

IO (Input Output) stream

IO Stream used to handle between Devices The data transfer

Java Operations on data is through Flow the way that flow is a means of manipulating data, technology

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/82/DA/wKiom1diijyRpEM_AABV6Y8s5IY242.png "title=" 1 "alt= "Wkiom1diijyrpem_aabv6y8s5iy242.png"/>


Flow There are two types of data by operation: Byte Stream and character stream.

Flow The flow is divided into: input stream, output stream.

character Stream The origin is: early byte stream + encoding table, in order to more easily manipulate the text data.

 

Remember: If you manipulate character data, you should prioritize the stream of characters.

manipulate other data and use only byte streams


Common base classes for IO streams

Byte stream abstract base class for:

InputStream ,outputstream.

character Stream abstract base class for:

Reader , Writer.

Note: The subclass names derived from these four classes are the suffixes whose parent class name is the child class name. Prefix names are the function of this subclass.

such as:inputstream sub-class FileInputStream.

such as:Reader subclass filereader.

 package cn.itcast.p4.io.file; import java.io.FileWriter;import java.io.IOException;  public class FileWriterDemo { /** *  @param  args *  @throws  exception  */    public static void main (String[] args )  throws Exception {                  /*         *  Requirement: Writes the in-memory string data to the hard disk.          * 1, memory data to the hard disk, should be output, that is, write. The output stream needs to be used.          * 2, the text data to be manipulated. Should be a character stream.          * 3, in summary, the character output stream should be used. Writer.          * 4, because the data is written to the hard disk, and the way the hard disk stores the data is the file.          * 5, you should find a character output stream object that can manipulate the file. Found the FileWriter.          */                 //1, create the FileWriter object.         /*         *   creates the object, the runtime creates the specified file, because the data must be written to the destination of the data store first.          *  Overwrite if the file that you want to create already exists.          */         Filewriter fw = new filewriter ("Demo.txt");                 //2, writes a string. is written to the buffer.         fw.write ("hehe");                 //3, use the Flush method. Writes the data in the buffer to the destination.         fw.flush ();              &nbSP;          //4, close  close         fw.close ();         }     }

io- character output stream _flush&close

* What is the difference between close () and flush () ?

* 1, call flush, the data can be brushed to the destination, the stream still exists, you can continue to use.

* call close, the data can be brushed to the destination, the stream has been closed. Cannot continue to use. Last use.

* 2, close not only flushes the buffer, but also shuts down the underlying resource .

processing of io- character output stream _io exception

package cn.itcast.p5.ioexception; import java.io.filewriter;import java.io.ioexception;  public class IOExceptionDemo {    public static void  Main (String[] args)  {              filewriter fw = null;        try {             fw = new filewriter ("Demo1.txt");                           fw.write ("ABCDE");             fw.flush ();        } catch  ( Exception e)  {             E.printstacktrace ();         } finally {                          if  (fw !=  NULL)                 try  {                     fw.close ();                 } catch  (ioexception e)  {                     throw new runtimeexception (" Shutdown failed ");            }         }         }     }


io- character Output stream _ resume & line break

package cn.itcast.p4.io.file; import java.io.filewriter;import java.io.ioexception;  Public class filewriterdemo2 {         private  static final String LINE_SEPARATOR =          system.getproperty ("Line.separator");                  /**         *  @param  args         *  @throws  IOException           */        public  Static void main (String[] args)  throws IOException {                  /*          *&nIf, at construction time, the second parameter passed to true, it will be continued at the end of the original file.          */         Filewriter fw = new filewriter ("Demo.txt", True);                  fw.write (line_separator+ "Hiahia");                  fw.close ();     } }






















io-overview-Inputs and outputs

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.