Java IO byte stream and character stream (ii)

Source: Internet
Author: User
Tags finally block

Overview

IO streams are used to process data transfer between devices

How Java operates on data by streaming

The objects that Java uses to manipulate the flow are in the IO package

Stream-by-operation data is divided into: byte stream and character stream

Flow is divided into: input stream and output stream by different flow

common base classes for IO streams

Abstract base class for byte stream

Inputstream,ouputstream

Abstract base class for character streams

Reader,writer

Note:

The subclasses of these base classes end with the names of the base classes, such as Filereader,fileinputstream

BYTE stream

BYTE stream: character-oriented stream

A simple example of how to copy a byte stream to a picture is shown

   1:import java.io.*;
   
   
   4: {
   5: Public     static void Main (string[] args) throws IOException
   6:     {
   7:         //Create byte file read stream
   8:         FileInputStream fis = new FileInputStream ("Pic1.png");
   9:         //Create byte file input stream
  Ten:         fileoutputstream fos = new FileOutputStream ("Pic2.png");
  
  :         //define Buffer
  :         byte[] buf = new byte[1024];
  14:         
  :         //Read byte to buffer and write to output stream, i.e. write to file
  :         int len =-1;
  : While         (len = Fis.read (BUF))! =-1)
  :         {
  :             fos.write (Buf,0,len);
  :         }
  21st:         
  :         //Close Resources
  At:         Fis.close ();
  :         fos.close ();
  25:         
  :     }
  27:}
character Stream

Characters stream: character-oriented stream

Character stream can only manipulate text, below is the example of character stream copy text file

   1:import java.io.*;
   
   
   4: {
   5: Public     static void Main (string[] args) throws IOException
   6:     {
   7:         //Create character file input stream
   8:         FileReader fr = new FileReader ("Iodemo1.java");
   9:         
  :         //Create character file output stream
  One:         FileWriter fw = new FileWriter ("Iodemo2.java");
  12:         
  :         //character buffer
  :         char[] data = new char[1024];
  15:         
  :         int len =-1;
  17:         
  :         //read in characters and write to file
  : While         (len = fr.read (data))! =-1)
  :         {
  :             fw.write (Data,0,len);
  :         }
  
  :         //Close Resources
  :         fw.close ();
  :         fr.close ();
  27:         
  :     }
  29:}
Convert Stream

Inputstreamreader,outputstreamwriter

InputStreamReader(InputStream in):接收字节流,转换为字符流

InputStreamReader(InputStream in, Charset cs):接收字节流,按照指定的编码表转换为字符流

In fact, FileReader is the use of the System default encoding table of the conversion stream, its internal use is also a byte stream, so the following two sentences are the same

FileReader FR = new FileReader ("1.txt");

InputStreamReader ISR = new InputStreamReader (New FileInputStream ("1.txt"), "GDK");

buffer and decorative design patternsbuffers

As the name implies, a buffer of data, reducing the coupling between the flow, reducing the communication between each and the hard disk devices, providing efficiency

The Bufferedwriter,bufferedreader,bufferedinputstream,bufferedoutputstream implements a buffer for the corresponding stream (in fact, an array is defined internally, Whether you read an array or a word, the inside will read the buffer array good one large enough for you to use.

Decorative Design Pattern

When we strengthen the function of a class in a system, if we define its subclass for each class, it will make the whole system appear very bloated, and the expansibility is not good, there is not a new class, it is necessary to create a subclass for the function enhancement, the decoration class appears all the time. This decoration class is to the class of a system to strengthen the function, when used in the System class object, and this decoration class should belong to this system

For example: BufferedReader, to strengthen all classes in the reader system, add buffer technology, the interface of the method is used in the parent class reference, using polymorphism to improve the function of extensibility

The evolution process of decorative design pattern

IO exception Handling mode

The exception base class for IO is IOException, which is a subclass of exception, representing the need to be handled by programmers

Because IO involves a variety of resources, you must close the resource operation in the finally block when try Processing

   1:/*
   2:io Exception Handling
   3: */
   
   5:import java.io.*;
   
   
   8: {
   9:     
  Ten:     {
  One:         FileWriter fw = null;
  :         FileReader fr = null;
  :         Try
  :         {
  :             fw = new FileWriter ("IODemo1.txt");
  :             fr = new FileReader ("IODemo2.txt");
  :             Fw.write (Fr.read ());
  
  :         }
  :         catch (IOException e)
  :         {
  :             System.out.println (E.tostring ());
  :         }
  :         finally
  :         {
  :             if (fw! = null)
  :             {
  :                 Try
  :                 {
  :                     fw.close ();
  :                 }
  :                 catch (IOException e)
  :                 {
  :                     System.out.println ("Close exception!");
  :                 }
  :             }
  PNS:             if (fr! = null)
  :             {
  :                 Try
  Max:                 {
  In:                     fr.close ();
  :                 }
  :                 catch (IOException e)
  :                 {
  :                     System.out.println ("Close exception!");
  :                 }
  :             }
  :         }
  :     }
  50:}

Java IO byte stream and character stream (ii)

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.