Let's talk about Java I/O, java

Source: Internet
Author: User

Let's talk about Java I/O, java

In java, I/O operations are classified into two categories at the macro level. One isByte stream,One is the upstream stream, which corresponds to a group of classes and interfaces respectively.


Byte stream:

Base class of InputStream input stream

Base class of OutputStream output stream

For byte streams, these two classes are the most basic classes.


Ghost stream:

Reader input stream base class

Base class of Writer output stream

It corresponds to two base classes in the byte stream, and the byte stream also has two closest classes.


There are two adapter classes in the sequence stream class group, which are used to convert byte streams into inverted streams, that is, converting from InputStream to Inverted Reader from OutputStream to Inverted Writer

They are:

InputStreamReader

OutputStreamWriter


In the programming process, whether the use of the byte stream or the byte stream needs to be determined based on the actual situation.



Java I/o Problems

DataOutputStream out = new DataOutputStream (
New FileOutputStream ("test. dat "));
DataInputStream in = new DataInputStream (
// ********** Found **********
New FileInputStream ("(" test. dat "));

In. closed ();

Java I/O Problems

Import java. io. DataOutputStream;
Import java. io. File;
Import java. io. FileOutputStream;
Import java. io. IOException;

Public class IO {
Public static void main (String args []) {
// Raw data
Double [] nums = {2.23, 4.67, 6.54, 2.22, 1.45, 5.64 };

// Save the data to the current folder named save. dat.
File file = new File (System. getProperty ("user. dir") + "\ save. dat ");

FileOutputStream fos = null;
DataOutputStream dos = null;

Try {
// If the object does not exist, create a new object.
If (! File. exists ())
File. createNewFile ();
// Create File streams and data streams
Fos = new FileOutputStream (file );
Dos = new DataOutputStream (fos );
// Write data
For (int I = 0; I <nums. length; I ++ ){
Dos. writeDouble (nums [I]);
}
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
// The operation ends. Close two streams.
Try {
If (dos! = Null)
Dos. close ();
If (fos! = Null)
Fos. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
}

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.