Java input/output stream (3): java input/output stream

Source: Internet
Author: User

Java input/output stream (3): java input/output stream

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:Corresponds to ByteArrayInputStream
  3) StringReader:Corresponds to StringBufferInputStream
  4) InputStreamReader
Read bytes from the input stream and convert them into characters: Public inputstreamReader (inputstream is );
  5) FilterReader:Allow to filter the batch stream
Protected filterReader (Reader r );
  6) BufferReader:Take the Reader object as a parameter and add a character buffer to it. You can use readline () to read a row.
Public BufferReader (Reader r );

Main Methods:

(1) public int read () throws IOException; // read a character. The returned value is the read character.

(2) public int read (char cbuf []) throws IOException;/* read a series of characters to the array cbuf [], and return the number of characters actually read */
(3) public abstract int read (char cbuf [], int off, int len) throws IOException;
/* Read len characters and store them from the subscript off of the array cbuf []. The returned value is the number of characters actually read. This method must be implemented by sub-classes */

2. Writer abstract class

The abstract class that writes streams. The sub-classes must implement only write (char [], int, int), flush (), and close () methods (). However, most subclasses will override some of the methods defined here to provide higher efficiency and/or other functions. Its subclass is as follows:

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.


Java input/output stream (adopted as the answer plus 100 points)

Input and Output
1. stream represents any data source capable of producing data or any receiving source capable of receiving data. In Java I/O, all streams (including Input and Out stream) have two types:

1.1 byte-oriented stream
A byte-oriented stream that reads or writes information to a stream in bytes. Byte-oriented streams include the following types:
1) input stream:
1) ByteArrayInputStream: uses a buffer in the memory as an InputStream.
2) StringBufferInputStream: Use a String object as InputStream.
3) FileInputStream: uses a file as InputStream to read the file.
4) PipedInputStream: implements the pipe concept and is mainly used in the process.
5) SequenceInputStream: combines multiple inputstreams into one InputStream.
2) Out stream
1) ByteArrayOutputStream: stores information in a buffer zone in the memory.
2) FileOutputStream: stores information in a file.
3) PipedOutputStream: implements the concept of pipe, which is mainly used in the process.
4) SequenceOutputStream: combines multiple outstreams into one OutStream.
1.2 Unicode Character-oriented stream
A Unicode-oriented stream that reads or writes information from a stream in units of Unicode characters. Unicode-oriented streams include the following types:
1) Input Stream
1) CharArrayReader: corresponds to ByteArrayInputStream
2) StringReader: corresponds to StringBufferInputStream
3) FileReader: corresponds to FileInputStream
4) PipedReader: corresponds to PipedInputStream.
2) Out Stream
1) CharArrayWrite: corresponds to ByteArrayOutputStream
2) StringWrite: no corresponding byte-oriented stream
3) FileWrite: corresponds to FileOutputStream
4) PipedWrite: corresponds to PipedOutputStream
Character-oriented stream basically corresponds to a byte-oriented stream. The functions of the two corresponding classes are the same, and the words are different in operation.
For example, CharArrayReader and ByteArrayInputStream both use a buffer in the memory as InputStream. The difference is that the former reads a byte of information from the memory each time, the latter reads one character from the memory each time.
1.3 conversions between two non-existing stream types
InputStreamReader and OutputStreamReader: convert a byte-oriented stream into a character-oriented stream.
2. Add attributes to stream
2.1 role of "adding attributes for stream"
Using the I/O APIs in Java described above, we can complete any operation we want to complete. However, through the sub-classes of FilterInputStream and FilterOutStream, we can add attributes for stream. The following is an example to illustrate the role of this function.
If we want to go to the full text>

Java input/output stream

Package;

Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. OutputStream;

Public class MyFileReader {
Public static void main (String [] args) throws IOException {
File sf = new File ("D:/IODemo1.txt"); // note that the File path is correct. The test is placed on the D Drive.
File df = new File ("D:/IODemo2.txt ");
MyFileReader. copy (sf, df );
}
Public static void copy (File srcfile, File desfile ){
Try {
InputStream is = new FileInputStream (srcfile );
OutputStream OS = new FileOutputStream (desfile );
Int c;
While (c = is. read ())! =-1 ){
OS. write (c );
}
Is. close ();
OS. flush ();
OS. close ();
} Catch (FileNotFoundException e ){
E. printStackTrace ();
System. out. println ("file not found ");
} Catch (IOException e ){
E. printStackTrace ();
System. out. println ("file input/output exception ");

}
}
}

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.