Java input and output

Source: Internet
Author: User

Java Stream (stream), file, and IO:

The 1.java.io package contains almost all the required classes for operation input and output. All of these flow classes represent the input source and output destination.

the streams in the 2.java.io package support many formats, such as basic types, objects, localized character sets, and so on.

3. A stream can be understood as a sequence of data. The input stream represents reading data from one source, and the output stream represents writing data to a target.

The 4.Java provides powerful and flexible support for I/O , making it more widely used in file transfer and network programming.

5. However, this section describes the most basic and current -I/O -related features. We will learn these features through an example.

Read console input:

Java console input is completed by system.in .

to get a character stream bound to the console, you can put System.in wrapped in a bufferedreader object to create a character stream.

Here's how to create basic syntax for BufferedReader:

BufferedReader br = new BufferedReader (new

InputStreamReader (system.in));

Once the BufferedReader object is created, we can use the read () method to read a character from the console or to read a string with the readLine () method.

To read multi-character input from the console:

to read a character from a BufferedReader object to use the Read () method , its syntax is as follows:

int read () throws IOException

each time the read () method is called, it reads a character from the input stream and returns the character as an integer value . Returns-1 when the stream ends . The method throws IOException.

The following program demonstrates the use of the Read () method to continuously read characters from the console until the user enters "q".

import java.io.*;

Public class Brread {

to read a character from a BufferedReader object to use the read () method, its syntax is as follows:

int Read () throws IOException

In simple terms, add throws to the back of the method IOException

Public Static void Main (string[] args) throws IOException {

TODO auto-generated Method stub

Char c;

Create bufferedreader using system.in

Once the BufferedReader object is created, we can use the read () method to read a character from the console

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

System. out. println (" input character, press ' q ' key to exit.) ");

Read characters

Do {

c= (char) br.read ();// Read and output

System. out. println (c);

}while (c!= ' Q ');// when the read character is Q , jump out of the loop

}

}

Console Output :

1. the output of the console is completed by print () and println () . These methods are defined by the class PrintStream , andSystem.out is a reference to the class object.

2.PrintStream inherits the OutputStream class and implements the method write (). In this way,write() can also be used for writing to the console.

The simplest format for the 3.PrintStream definition of write () is as follows:

void write (int byteval)

This method writes the low eight-bit bytes of the byteval to the stream.

Java input and output

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.