Character stream with byte stream (IO)

Source: Internet
Author: User
Tags uppercase character

Character Stream and Byte stream

One, characters commonly used stream:

Write:

FileWriter: Used to create a new file that writes characters

BufferedWriter: Writing text to character output stream

Example: BufferedWriter s=new bufferedwriter (New FileWriter ("Abc.txt"));

Read:

FileReader: Used to create a new file that reads characters

BufferedReader: Reading text into the input stream

Example: BufferedReader s=new BufferedReader (New FileReader ("Abc.txt"));

Second, characters commonly used throttling:

Write:

FileOutputStream: Used to create a file output stream for writing data to

Bufferedoutputstream: This class implements a buffered output stream.

Read:

FileInputStream: Obtaining input bytes from a file in the file system

Bufferedinputstream: This class implements a buffered input stream.

Cases:

Bufferedinputstream S=newbufferedinputstream (New FileInputStream ("Abc.txt"));

Third, the conversion flow:

OutputStreamWriter: A bridge of character flow to a byte stream: You can encode the characters in the stream to be written into bytes using the specified charset . The character set it uses can be specified by name or explicitly given, otherwise the platform default character set will be accepted.

InputStreamReader: A bridge that bytes flow to a character stream: it reads the bytes with the specified charset and decodes them to characters. The character set it uses can be specified or explicitly given by name, or it can accept the default character set of the platform.

Keyboard entry:

BufferedReader bufr = new BufferedReader (Newinputstreamreader (system.in));

Output to the console:

BufferedWriter BUFW = new BufferedWriter (Newoutputstreamwriter (System.out)); String line = null;

Through the loop of the while, the data is continuously entered through the keyboard, and the output is converted into uppercase to the console, when

When the input is over, the loop will end.

while ((Line=bufr.readline ())!=null) {

if ("Over". Equals (line))

Break

Bufw.write (Line.touppercase ());//convert input characters to uppercase character output

Bufw.newline ();

Bufw.flush ();

}

Bufw.close ();

Bufr.close ();

Convert stream = byte stream + encoding table.

The subclass of the transform stream is file = Byte stream + default encoding table.

FileReader FR = Newfilereader ("A.txt");

InputStreamReader ISR = Newinputstreamreader (New FileInputStream ("A.txt"), "GBK");

Iv. Other flows:

Bytearrayinputstream: At the time of construction, you need to receive the data source. And the data source is a byte array.

Bytearrayoutputstream: At construction time, you do not need to define the data purpose because the variable-length byte array is already encapsulated inside the object.

Because both of these stream objects manipulate arrays, system resources are not used.

Therefore, close is not done.

Cases:

import java.io.*;

class Bytearraystream

{

Public static void main (string[] args)

{

// The data source.

Bytearrayinputstream bis = newbytearrayinputstream (" haha is computer ". GetBytes ()) ;

// Data Purpose

Bytearrayoutputstream BOS = new bytearrayoutputstream ();

int len = 0;

The data read from the BIS is written to the BOS

while ((Len=bis.read ())!=-1)

{

Bos.write (len);

}

System. out. println (Bos.size ());

System. out. println (Bos.tostring ());

}

}

Pipe flow: Read and write operations for the JAva pipeline flow between two threads
PipedOutputStreamand PipedInputStream are connected together as a conduit.
The pipeline output stream can write data to the pipeline, which can read the data from the pipe,
This decorator decorative design pattern greatly enhances the functionality of the Java stream
You can connect a pipeline input and output stream while constructing a flow, or you can connect via the Connect function

PipedOutputStream

PipedInputStream

Cases:

Package IO21;

import java.io.*;

class Read implements Runnable

{

Private PipedInputStreamin;

Read (PipedInputStream in)

{

this. in =;

}

Public void run ()

{

Try

{

byte [] buf =new byte[1024];

System. out. println (" before reading: No data blocking ");

int len =in. Read (BUF);

System. out. println (" Read data: Blocking end ");

String s= New string (Buf,0,len);

System. out. println (s);

in. Close ();

}

Catch (IOException e)

{

throw new RuntimeException (" Pipe Read stream failed ");

}

}

}

Implementing the Runnable Interface

class Write implements Runnable

{

Overlay construction method, pipeline output stream object

Private PipedOutputStreamout;

Write (PipedOutputStream out)

{

this. out = out;

}

Replication Run method

Public void run ()

{

Try

{

System. out. println (" start writing data, wait 6 seconds.") ");

Thread. Sleep (6000);

out. Write ("piped Laila". GetBytes ());

out. Close ();

}

Catch (Exception e)

{

throw new RuntimeException (" pipe output stream failed ");

}

}

}

class Pipedstreamdemo

{

Public static void main (string[] args)throws IOException

{

Create a pipeline output stream object for writing data

PipedInputStream in = new pipedinputstream ();

Create a pipeline input object for reading text data

PipedOutputStream out = new pipedoutputstream ();

In.connect (out);//connect the pipeline output stream to the input stream for data reading and writing

Creating objects, opening threads

Read R = new read (in);

Write w = new write (out);

New Thread (R). Start ();

New Thread (W). Start ();

}

}

Print Flow: This stream provides a way to print the data of various data types as-is.

A: Provides more features, such as printing methods. You can print any type of data directly.

B: It has an automatic refresh mechanism that creates the object, specifies parameters, and can be automatically refreshed for the specified method.

C: It uses the native default character encoding.

D: The print method of the stream does not throw IOException.

BYTE print Stream (PrintStream):

A, File object: File

B, String Path: string

C, byte output stream: OutputStream

Character Print stream (PrintWriter):

A, File object: File

B, String Path: string

C, byte output stream: OutputStream

D, character output stream: Writer

Cases:

Package I020;

/*

Print Flow:

This flow provides printing methods that can print data of various data types as-is.

BYTE print stream:

PrintStream

Types of parameters that the constructor can receive:

1 , file object. File

2 a string path. String

3 a byte output stream. OutputStream

Character Print stream:

PrintWriter

Types of parameters that the constructor can receive:

1 , file object. File

2 a string path. String

3 a byte output stream. OutputStream

4 , character output stream, Writer .

*/

import java.io.*;

class Printstreamdemo

{

Public static void main (string[] args)throws IOException

{

Keyboard entry

BufferedReader BUFR =

New BufferedReader (new InputStreamReader (System. in));

Data is processed into the A.txt file and refreshed

PrintWriter out = new printwriter (new FileWriter ("A.txt"),true);

String line = null;

The keyboard input data into uppercase, and real-time written to the A.txt file, when encountered over, the end of keyboard input

while ((Line=bufr.readline ()) =null)

{

if ("Over". Equals (line))

break;

Out.println (Line.touppercase ());

}

Out.close ();

Bufr.close ();

}

}

Character stream with byte stream (IO)

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.