Learning java standard data streams and input/output streams

Source: Internet
Author: User

 

Learning java standard data streams and input/output streams

Study Notes song hexian

Data streams are classified into inputstream and outputscream.

The purpose of data flow is to make the input/output operations of a program independent of related devices. Because the implementation details of each device are executed by the system, you do not need to worry about these details in the program, so that a program can be used for multiple input/output devices, you do not need to make any changes to the source code or even the target code to enhance the program portability.

Java standard data stream

1. Standard Input: system. In

Public int read () throws ioexception: returns the read byte. If it reaches the end of the row,-1 is returned.

2. standard output system. Out

Public int read (byte [] I) throws ioexception: returns the total number of bytes read into the buffer. If data is no longer available because it has reached the end of the stream,-1 is returned.

An ioexception is thrown when an IO error occurs using the read () method.

3. Standard Error output system. Err

Byte stream

1. byte input stream inputstream class

The inputstream class is an abstract class and cannot directly generate objects. It is the parent class of all byte input stream classes. This class provides basic input processing methods, and its subclasses generally override these methods.

Note: Most methods in this class may throw an ioexception. Therefore, when calling them, try... Catch Block, capture and handle ioexception.

Data Reading Method

Int read () throws ioexception;

Int read (byte [] B) throws ioexception;

Int read (byte [] B, int off, int Len) throws ioexception;

Close input stream

Public void close () throws ioexception;

Obtains the number of readable bytes in a stream.

Public int available () throws ioexception;

Move read pointer

Public long SKIP (long n) throws ioexception;

Mark the location in the stream and reset the read location

Public Boolean marksupported ();

Public void mark (INT readlimit );

Public void reset ();

2. byte output stream outputstream

The outputstream class is an abstract class and cannot directly generate objects. It is the parent class of all byte output stream classes. This class provides basic methods for output processing, and its subclasses generally override these methods.

Data Output Method

Void write (int B) throws ioexception;

Void write (byte [] B) throws ioexception;

Void write (byte [] B, int off, int Len) throws ioexception;

Close output stream

Public void close () throws ioexception;

Clear the buffer

Public void flush () throws ioexception;

File byte input/output stream class

Filelnputstream and fileoutputstream are used to process file input and output, and their data objects are all files.

Fileinputstream

Fileinputstream is used to access local files in sequence. It inherits methods such as read () and close () from the parent class inputstream to operate the files on the machine, but does not support the mark () and reset () methods.

Constructor

Public fileinputstream (string name) throws filenotfoundexception

Public fileinputstream (File file) throws filenotfoundexception

Byte reading Method

Public int read () throws ioexception

Public int read (byte [] B) throws ioexception

Public int read (byte [] B, int off, int Len) throws ioexception

Example: open a file

Import java. Io .*;

Public class readfiletest

{

Public static void main (string [] ARGs) throws ioexception

{

Try

{

// Create a file input stream object

Fileinputstream FCM = new fileinputstream ("readfiletest. Java ");

Int n = FCM. Available ();

Byte B [] = new byte [N];

// Read the input stream

While (FCM. Read (B, 0, n ))! =-1)

{

System. Out. Print (new string (B ));

}

System. Out. println ();

// Close the input stream

FCM. Close ();

}

Catch (ioexception IOE)

{

System. Out. println (IOE. getmessage ());

}

Catch (exception E)

{

System. Out. println (E. getmessage ());

}

}

}

Use the available () method to obtain the number of bytes that can be read from the input stream, and then use the read (byte [] B) method to read from the source program file readfiletest. read files in Java are stored in byte array B, and then the new string (B) constructed with the value in B is displayed on the screen. When the program is running, the content of the source program file readfiletest. Java is displayed on the screen.

File byte output stream fileoutputstream class

The fileoutputstream class is used to write data to a file. It inherits methods such as write () and close () from the superclass outputstream.

Constructor

Public fileoutputstream (string name) throws filenotfoundexception

Public fileoutputstream (File file) throws filenotfoundexception

Public fileoutput. Stream (string name, Boolean append) throws filenotfoundexception

Byte Writing Method

Public void write (int B) throws ioexception

Public void write (byte [] B) throws ioexception

Public void write (byte [] B, int off, int Len) throws ioexception

Example 12.3 writing a file

Import java. Io .*;

Public class writefiletest

{

Public static void main (string [] Arg)

{

Try

{

System. Out. println ("Please input :");

Int count;

Byte B [] = new byte [512];

Count = system. In. Read (B );

// Read the standard input stream

Fileoutputstream Fos = new fileoutputstream ("data.txt ");

// Create a file output stream object

FOS. Write (B, 0, count );

// Write a file

FOS. Close ();

// Close the output stream

System. Out. println ("the file is saved successfully! ");

}

Catch (ioexception IOE)

{

System. Out. println (IOE. getmessage ());

}

}

}

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.