Java I/O operations (iii) __java

Source: Internet
Author: User

Java I/O operations (iii)

The following short paragraph (before the divider line) is quoted in the fourth edition of the "in Java" chapter tenth, as a review

"10.1 Input and output

You can split the IO class of the Java library into two parts of input and output, as you would know when reading an online Java class document in a Web browser. By inheritance, all classes derived from InputStream (input stream) have a basic method named Read (), which is used to read a single byte or array of bytes. Similarly, all classes derived from OutputStream have the basic method write (), which is used to write a single byte or byte array. However, we don't usually use these methods; they exist because more complex classes can take advantage of them to provide a more useful interface. Therefore, we rarely create our own system objects with a single class. In general, we all overlap multiple objects to provide the functionality we expect. We feel that Java's stream library is unusually complex because of the need to create multiple objects in order to create a single result stream.

It is necessary to categorize classes by function. The designer of the library first decides that all classes related to the input inherit from InputStream, while all the classes associated with the output inherit from OutputStream. "

-----------------------------------------------separated------------------------------------------

Java's IO operations are complicated by the creation of a class that must be associated with many other classes, that is, a stream and a stream, forming a link to a similar pipeline, which is an application of "decorative mode", an English name called decorator.

such as:fileinputstream->bufferdinputstream->datainputstream;

dataoutputstream->buffedoutputstream->fileoutputstream;

-----------------------------------------------------------------------------------------------

java.io.charset.*;

Common Character Set Categories:

1.ASCII is written by American Standard Code Information Interchange, is the U.S. Information Interchange standard code, one byte per character. Commonly used 0~127 (decimal), this part is first in 0, the remainder represents special characters, for the difference and the front section, with 1 opening. More general.

2.gb2312 commonly used in text encoding, two bytes per character

3.GBK, fully compatible with GB2312, and encode traditional and other non-commonly-expressed characters as well as special symbols;

The encoding format of the character:

Unicode: For Java, each character is represented by two bytes.

UTF-8: A more flexible encoding that determines how many bytes are used to represent a character based on the size of the binary number of characters.

-------------------------------------------------------------------------------------------------

Reader and writer that manipulate character streams

Java unifies Unicode to encode characters, each character occupies two bytes, 16 bits

InputStreamReader and OutputStreamWriter

Inputstreamreade, constructed from an already existing InputStream object

Serves as a bridge to convert a stream of bytes to a character, which can be specified at construction time (CharSet)

OutputStreamWriter, constructed from an already existing OutputStream object.

A bridge that acts as a stream of characters into byte streams, which can be specified at construction time (CharSet)

Another concept is introduced:

Converting a stream of characters into a stream of bytes is encoded, and converting the streams of bytes into character streams is called decoding.

BufferedReader and BufferedWriter

Provide buffered read and write, improve efficiency. Constructed from an already existing reader and writer respectively.

------------------------------------------

Note: The problem that the text does not display correctly is that the decoding and coding rules do not match. Conversions between different codes are generally irreversible, such as the conversion of a two-byte GB2312 to one-byte ASCII code that results in data loss. However, the corresponding character stream conversion can be converted into bytecode after unification.

------------------------------------------

Code:

Create the Test.txt file and make the Chinese character entry. The meaning of Import java.nio.charset.* is that it can explain GB2312.

Import java.io.*;

Import java.nio.charset.*;

public class Unicodetest

{

public static void Main (string[] args) throws Exception

{

FileOutputStream fops=new FileOutputStream ("test.txt");

OutputStreamWriter opsw=new OutputStreamWriter (fops, "GB2312");

BufferedWriter bw=new BufferedWriter (OPSW);

InputStreamReader ipsr=new InputStreamReader (system.in, "GB2312");

BufferedReader br=new BufferedReader (IPSR);

String[] Str=new string[10];

int i=0;

while (I<10)

{

Str[i]=br.readline ();

i++;

if (i==10)

System.out.println ("Enough Now");

}

i=0;

while (I<10)

{

Bw.write (Str[i],0,str[i].length ());

Bw.flush ();

Bw.newline ();

System.out.println ("Write line" +i+ "completely");

i++;

}

}

}

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.