Java input and output stream 3

Source: Internet
Author: User

8. Character Stream Writer/reader

the characters in Java are in the Unicode standard, one character is 16 bits, that is, one character is represented by two bytes. To do this, a stream that handles characters is introduced in Java.

1. Reader abstract class

Abstract class for reading stream of characters. Subclasses must implement only read (char[], int, int) and close (). However, most subclasses will override some of the methods defined here to provide higher efficiency and/or other functionality.

1) FileReader:Corresponds to FileInputStream
Mainly used to read character files, using the default character encoding, there are three kinds of constructors:
(1) The file name as a string: FileReader f=new filereader ("C:/temp.txt");
(2) The constructor takes the file object as its argument.
File F=new file ("C:/temp.txt");
FileReader f1=new FileReader (f);
(3) The constructor filedescriptor the object as a parameter
FileDescriptor () fd=new filedescriptor ()
FileReader f2=new FileReader (FD);
(1) using the specified character array as the parameter: CharArrayReader (char[])
(2) Character array as input stream: CharArrayReader (char[], int, int)
To read the string, the constructor is as follows: Public StringReader (string s);
2) CharArrayReader:Corresponds to Bytearrayinputstream
   3) StringReader:Corresponds to StringBufferInputStream
   4) InputStreamReader
Reads bytes from the input stream, converting them to characters: public InputStreamReader (InputStream);
   5) FilterReader:Allow filtering of character streams
Protected FilterReader (Reader R);
   6) Bufferreader:Accepts the reader object as a parameter and adds a character buffer to it, using the ReadLine () method to read a row.
Public Bufferreader (Reader R);

Main methods:

(1) public int read () throws IOException;//read one character, return value read character

(2) public int read (char cbuf[]) throws IOException; /* Reads a series of characters into the array cbuf[], the return value is the number of characters actually read */
(3) public abstract int Read (char cbuf[],int off,int len) throws IOException;
/ * Reads Len characters, starts at the subscript off of the array cbuf[], and returns the number of characters actually read, which must be implemented by the subclass * /

2. Writer Abstract class

Abstract class that writes a stream of characters. Subclasses must implement methods that have only write (char[], int, int), flush (), and Close (). However, most subclasses will override some of the methods defined here to provide higher efficiency and/or other functionality. Its subclasses are as follows:

1) FileWrite: corresponds to FileOutputStream
Writes character type data to a file, using the default character encoding and buffer size.
Public FileWrite (file f);
  2)  chararraywrite: Corresponding to the Bytearrayoutputstream, the uses the character buffer as the output.  
   public chararraywrite ();  
3) printwrite: generate formatted output  
   public printwriter (outputstream os)  
4) Filterwriter : Used to write filter character stream  
   protected Filterwriter (Writer w);  
PipedOutputStream corresponds to    

6) StringWriter: No corresponding byte-oriented stream

Main methods:

(1) public void write (int c) throws IOException;  //Writes the lower 16 bits of the integer value c to the output stream
(2) public void Write (char cbuf[]) throws IOException;//write character array cbuf[] to the output stream
(3) public abstract void Write (char cbuf[],int off,int len) throws IOException;  //Writes Len characters starting at the position of the character array cbuf[] from the index off to the output stream
(4) public void write (String str) throws IOException;  //writes the characters in the string str to the output stream
(5) public void write (String str,int off,int len) throws IOException;  //writes the string str from the Len character at the beginning of the index off to the output stream
(6) flush ()//  The empty output stream is brushed and all cached bytes are output.
(7) Close() closed stream  Public abstract void Close () throws IOException

3. difference between InputStream and reader between OutputStream and writer

inputstream and OutputStream classes deal with byte streams, The smallest unit in the data flow is the byte (8 bit)
Reader and writer are dealing with a character stream, which involves the conversion of character encoding when processing a character stream

import java.io.*;  public class encodetest {       Private static void readbuff (Byte [] buff)  throws IOException {          ByteArrayInputStream in =new  Bytearrayinputstream (Buff);          int data;           while ((Data=in.read ())!=-1)     System.out.print (data+ "  ");           System.out.println ();      in.close ();     }        public static void main (string args[])  throws  Ioexception {         system.out.println ("Unicode character encoding in memory:"  );          chAr   c= ' Good ';         int lowbit=c&0xff;      int highbit= (C&0XFF00) >>8;          system.out.println ("+lowbit+"     "+highbit");          string s= "Good";          SYSTEM.OUT.PRINTLN ("Local operating system default character encoding:");          readbuff (S.getbytes ( );          system.out.println ("Using GBK character encoding:");          readbuff (S.getbytes ("GBK"));          system.out.println ("With UTF-8 character encoding:");                readbuff (S.getbytes ("UTF-8"));       }  }


Reader classes can convert characters in the input stream that use other encoding types to Unicode characters, and then allocate memory for them in memory
The writer class is able to convert in-memory Unicode characters to characters of other encoded types, and then write to the output stream.

9. Subclasses of the IOException exception class

1.public class Eofexception:
This type of exception is thrown when the end of the file is not properly reached or at the end of the input stream.
2.public class FileNotFoundException:
The exception that is thrown when the file is not found.
3.public class Interruptedioexception:
This type of exception is thrown when the I/O operation is interrupted.


Java input and output stream 3

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.