Java input/output stream (3)

Source: Internet
Author: User

Java input/output stream (3)

8. bytes stream Writer/Reader

In Java, the character adopts the Unicode standard. A character is a 16-bit character, that is, a character is expressed in two bytes. To this end, JAVA introduces Stream processing characters.

1. Reader abstract class

The abstract class used to read the livestream. The sub-classes must implement only read (char [], int, int) and close () methods (). However, most subclasses will override some of the methods defined here to provide higher efficiency and/or other functions.

1) FileReader:Corresponds to FileInputStream
It is mainly used to read character files and uses the default character encoding. There are three constructors:
(1) use the file name as the string: FileReader f = new FileReader ("c:/temp.txt ");
(2) The constructor uses the File object as its parameter.
File f = new file ("c:/temp.txt ");
FileReader f1 = new FileReader (f );
(3) The constructor uses the FileDescriptor object as the parameter.
FileDescriptor () fd = new FileDescriptor ()
FileReader f2 = new FileReader (fd );
(1) Use the specified character array as the parameter: CharArrayReader (char [])
(2) Use the character array as the input stream: CharArrayReader (char [], int, int)
Read the String. The constructor is as follows: public StringReader (String s );
2) CharArrayReader: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Release + release/vB9yA8YnI + release/z1/release + release/sgPC9zdHJvbmc + PGJyPgo8L3A + release/cG/release + 6 OstNPK/release + 8r9wb + incubator/WKi8gPC9wPgo8aDM + CjIuIFdyaXRlcrPpz/PA4DwvaDM + incubator/incubator + incubator/Co7o8L3A + Incubator "http://www.2cto.com/uploadfile/Collfiles/20140815/20140815092141321.gif" alt = "\">

1) FileWrite: corresponds to FileOutputStream
Write Data of character type into file, use default character encoding and buffer size.
Public FileWrite (file f );
2) chararrayWrite: corresponds to ByteArrayOutputStream and uses the character buffer as the output.
Public CharArrayWrite ();
3) PrintWrite: Generate formatted output
Public PrintWriter (outputstream OS );
4) filterWriter: used to write and filter the shard stream.
Protected FilterWriter (Writer w );
5) PipedWriter: corresponds to PipedOutputStream.

6) StringWriter: no corresponding byte-oriented stream

Main Methods:

(1) public void write (int c) throws IOException; // write the low 16 bits of integer c to the output stream
(2) public void write (char cbuf []) throws IOException; // write the character array cbuf [] to the output stream
(3) public abstract void write (char cbuf [], int off, int len) throws IOException; // write the len characters in the character array cbuf [] from the position where the index is off to the output stream.
(4) public void write (String str) throws IOException; // write the characters in String str to the output stream.
(5) public void write (String str, int off, int len) throws IOException; // write the len characters starting from index off in String str to the output stream.
(6) flush () // flush the empty output stream and output All cached bytes.
(7) close () close the stream public abstract void close () throws IOException

3. Differences between InputStream and Reader: differences between OutputStream and Writer

The InputStream and OutputStream classes process byte streams. The minimum unit of data streams is byte (8 bits)
Reader and Writer process the character stream, which involves character encoding conversion.

  1. Import java. io .*;
  2. Public class EncodeTest {
  3. Private static void readBuff (byte [] buff) throws IOException {
  4. ByteArrayInputStream in = new ByteArrayInputStream (buff );
  5. Int data;
  6. While (data = in. read ())! =-1) System. out. print (data + "");
  7. System. out. println (); in. close ();}
  8. Public static void main (String args []) throws IOException {
  9. System. out. println ("unicode character encoding in memory :");
  10. Char c = 'ha ';
  11. Int lowBit = c & 0xFF; int highBit = (c & 0xFF00)> 8;
  12. System. out. println ("" + lowBit + "" + highBit );
  13. String s = "good ";
  14. System. out. println ("Local Operating System Default character encoding :");
  15. ReadBuff (s. getBytes ());
  16. System. out. println ("using GBK character encoding :");
  17. ReadBuff (s. getBytes ("GBK "));
  18. System. out. println ("UTF-8 character encoding :");
  19. ReadBuff (s. getBytes ("UTF-8 "));}
  20. }
    The Reader class can convert characters of Other encoding types in the input stream to Unicode characters, and then allocate memory for them in the memory.
    The Writer class can convert Unicode characters in memory to other encoding types before writing them to the output stream.

    9. Subclass of the IOException class

    1. public class EOFException:
    This type of exception is thrown when it reaches the end of a file or input stream abnormally.
    2. public class FileNotFoundException:
    An exception is thrown when the file cannot be found.
    3. public class InterruptedIOException:
    This type of exception is thrown when I/O operations are interrupted.

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.