Java demonstrates how to redirect to a file with a standard input-output stream

Source: Internet
Author: User

Recently, learning Java, and learning about Java IO (input and input), what is Java IO?
IO flow is located in the Java.io packet, according to the operation data, divided into byte stream and character stream, according to different data input and can be divided into the input stream and output stream, regardless of what kind of flow, ultimately rely on the operating system.
One, the word stream:
1, byte stream, mainly used for picture, audio, video transmission, in the form of binary, divided into bytes input stream and byte output stream; The byte stream is an array of bytes, and a character stream operates as an array of characters.
2, byte input and byte output stream inheritance system diagram

InputStream Common methods
Method declaration
Function description
int read ()
Reads a 8-bit byte from the input stream, converts it to an integer between 0-255, and returns this integer
int read (byte[] b)
Reads several bytes from the input stream, saves them to the byte array specified in Parameter B, and returns an integer representing the number of bytes read
int read (byte[] b,int Off,len)
Reads several bytes from the input stream, saves them to the byte array specified in Parameter B, and off specifies the starting subscript for the byte array to start saving data, and Len represents the bytes read
void Close ()
Closes this input stream and frees all system resources associated with the stream

OutputStream Common methods
Method declaration
Function description
void write (int b)
Writes a byte to the output stream
void Write (byte[] b)
Writes all bytes of the byte array specified by parameter B to the output stream
void Write (byte[] B,int Off,len)
Writes Len bytes from offset off to the output stream in a specified byte array
void Flush ()
Refreshes this output stream and forces all buffered output bytes to be written out
void Close ()
Closes this output stream and frees all system resources associated with the stream

BYTE stream read-write file:
FileInputStream is a byte input stream that is used to manipulate files, specifically for reading data from a file.
The FileOutputStream is a byte output stream that is used to write data to a file.

The code is as follows:

package Redirecting;import java.io.*;public class Redirecting {    public static void main(String[] args) throws IOException {        PrintStream console = System.out;        BufferedInputStream in = new BufferedInputStream(new FileInputStream("/Users/mac/IdeaProjects/JF_Java_EDU/src/Redirecting/Redirecting.java"));        PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("outtest.txt")));        System.setIn(in);        System.setOut(out);        System.setErr(out);        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        String s;        while ((s = br.readLine()) != null)            System.out.println(s);                out.println(s);        out.close();        System.setOut(console);    }}

FileReader and FileWriter are used to read and write files; BufferedReader and bufferdewriter are buffer-capable streams that improve read and write efficiency.

An important method in BufferedReader is Readlind (), which is used to read one line of text at a time.

The conversion stream is a stream of characters that can only be used when the byte stream reads and writes text data, by converting the flow to use the character efficient stream method. And can not achieve the picture, audio and other data read and write.
InputStreamReader: The bridge that understands the byte flow to a character stream, used as:
BufferedReader br = new BufferedReader (new InputStreamReader (system.in));
OutputStreamWriter: Understanding is a bridge of character flow to a byte stream, using or passing bytes into a stream of characters:
BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System.out));

The final output, the output of your code to a text document, as follows:

Java demonstrates how to redirect to a file with a standard input-output stream

Related Article

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.