Java IO (3)

Source: Internet
Author: User

Character Stream correlation

A stream of characters can basically be analogous to a byte stream except that it converts bytes of bytes into char.

The two most fundamental classes are reader and writer

The subclasses of reader are:
BufferedReader, CharArrayReader, FilterReader, InputStreamReader, Pipedreader, StringReader

The subclasses of writer are:

BufferedWriter, Chararraywriter, Filterwriter, OutputStreamWriter, PipedWriter, PrintWriter, StringWriter

Special classes:

InputStreamReader and OutputStreamWriter Use these two classes to convert between a byte stream and a character stream, provided that the string is read in itself.

Convert bytes to character stream

Public InputStreamReader (InputStream in)

Converts bytes to a character stream using the specified encoding format CS

InputStreamReader (InputStream in, Charset CS)

The use of OutputStreamWriter is a similar way

Example:

Package com.characterStream.bufferWriter;

Import java.io.*;

public class Streamtest {

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

FileOutputStream fos=new FileOutputStream ("F:\\filetest.txt");

This converts bytes into a stream of characters.

OutputStreamWriter osw=new outputstreamwriter (FOS);

Here is the filter stream of the byte stream

BufferedWriter bw=new BufferedWriter (OSW);

Bw.write ("The Test of BufferedStream");

Bw.write (' \ n ');

Bw.write ("Test is finished");

When you use a character stream, close here becomes more critical.

Content in BufferedWriter is only written to the file when close is available

Otherwise, there's no real content in the file.

Bw.close ();

Then read from the outside through the character stream

FileReader fr=new FileReader ("F:\\filetest.txt");

BufferedReader br=new BufferedReader (FR);

This is one line of data per read

String str;

while ((Str=br.readline ())!=null) {

System.out.println (str);

}

Br.close ();

Packaging a standard input device into a byte stream the information entered on the control body will be printed again.

InputStreamReader isr=new InputStreamReader (system.in);

BufferedReader bfr=new BufferedReader (ISR);

Note that the general format of this input stream reads to the last line and then it comes out. A null indicates that the last line has been//reached

while ((Str=bfr.readline ())!=null) {

System.out.println (str);

}

Bfr.close ();

}

}

Note that due to the use of buffers in the stream of characters, it is critical that the contents of the stream be read or read, plus close, only at close.

On the application of packaging mode in IO

I've seen some typical examples of specific IO filter flows, such as:

DataOutputStream dos=new DataOutputStream (New Bufferedoutputstream (New FileOutputStream ("F:\\filetest.txt"));

But why this can be written, that is why it can be written in such a nested pattern, which involves the Java design pattern, which is applied to the packaging model is specific to see:

Http://www.cnblogs.com/Goden/p/3901664.html

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.