Summary of IO in Java

Source: Internet
Author: User

The IO stream in Java is divided into two main categories:

    • BYTE stream: Inputstream+outputstream
    • Character Stream: Reader+writer

BYTE stream:

    • InputStream is the parent class of all byte input streams
    • OutputStream is the parent class of all byte output streams
    • FileInputStream reading data from a file
    • Fileoutpustream writing data to a file
    • Bytearrayinputstream reading data from a byte array
    • Bytearrayoutputstream writing data to memory
    • PrintStream is a wrapper class that outputs data to a specified stream
    • System.in is an instance of the InputStream subclass that specifies reading data from the keyboard (can be redirected)
    • System.out/system.err is an instance of PrintStream that specifies that data is written to the screen (can be redirected)

Character Stream:

    • Reader all character input stream parent class
    • Writer all character output stream parent class
    • FileReader reading a string from a file
    • FileWriter outputting A string to a file
    • InputStreamReader reading characters from the InputStream
    • OutputStreamWriter character output to OutputStream
    • BufferedReader reading characters from a specified reader
    • BufferedWriter writes a character to the stream specified by writer

Here's an example of reading data from the keyboard and outputting it to the screen (using BufferedReader implementation)

ImportJava.util.*;ImportJava.io.*; Public classhello{ Public Static voidMain (string[] args) {bufferedreader br=NewBufferedReader (NewInputStreamReader (system.in)); Try{
System.out.print ("Please enter your message:"); String
Line
=Br.readline (); System.out.print (line); }Catch(IOException IoE) {ioe.printstacktrace (); } }}

Operation Result:

Please enter your Message:hello guys and nice to meet. My back hurts.
Hello guys, nice to meet. My back hurts.

Summary of IO in Java

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.