Summary of operation of IO (input and output) in Java (Iv.) _java

Source: Internet
Author: User
Tags anonymous object serialization serialization
The main operations of Java IO have been finished before
In this section, we'll talk about other aspects of Java IO

serializable serialization
Instance 1: Serialization of Objects
Copy Code code as follows:

Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.ObjectOutputStream;
Import java.io.Serializable;
@SuppressWarnings ("Serial")
To implement serialization, a class must implement the Serializable interface
Class Person implements Serializable {
private String name;
private int age;
Public person (String name, int age) {
THIS.name = name;
This.age = age;
}
Public String toString () {
Return "Name:" + THIS.name + ", Age:" + this.age;
}
}
public class Demo {
public static void Main (string[] args) {
String Path = file.separator + "Home" + File.separator + "SIU" +
File.separator + "Work" + File.separator + "Demo.txt";
person P1 = new Person ("Zhangsan", 12);
person P2 = new Person ("Lisi", 14);
The reference to creating a file write inflow here is to play with the ObjectOutputStream constructor
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
FOS = new FileOutputStream (path);
Oos = new ObjectOutputStream (FOS);
Here you can write to the object or write other types of data
Oos.writeobject (p1);
Oos.writeobject (p2);
catch (IOException e) {
E.printstacktrace ();
finally {
try {
Oos.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
}
}

Object serialization is the persistent storage of an object to preserve its attributes easily.
In layman's terms, it is equal to pulling an object out of the heap memory and putting it on the hard disk.
Of course, if you're happy, you can serialize other things, including arrays, basic data types, and so on.
Take a look at the contents of the horse thing.


Instance 2: Deserialization of an object

Copy Code code as follows:

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.ObjectInputStream;
public class Demo {
public static void Main (string[] args) {
String Path = file.separator + "Home" + File.separator + "SIU" +
File.separator + "Work" + File.separator + "Demo.txt";
Well, the code here is really a little bit longer, and it's going to throw up some weird stuff.
If you're bored with it, just throw it in the main method and use the anonymous object in the construction method.
What the? Don't tell me you don't know the anonymous object.
FileInputStream FIS = null;
ObjectInputStream ois = null;
try {
FIS = new FileInputStream (path);
OIS = new ObjectInputStream (FIS);
This is actually an object class.
Because we know it's a person class object.
So, place it in the downward transition
Person P = (person) ois.readobject ();
SYSTEM.OUT.PRINTLN (P);
I'm sick and tired of killing you ~!!!
catch (IOException e) {
E.printstacktrace ();
catch (ClassNotFoundException e) {
E.printstacktrace ();
finally {
try {
Still remember to close down the dirty
Ois.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
}
}

You see, we put an object on the hard drive to make it easier to use later.
You need it now, it's natural to take it out.


Pipe Flow
Instance 3: Communication of Threads

Copy Code code as follows:

Import java.io.IOException;
Import Java.io.PipedInputStream;
Import Java.io.PipedOutputStream;
Implement Runnable interface, implement a read thread
Class Read implements Runnable {
Private PipedInputStream in;
To spread the pipes that need to be read into the constructor
Public Read (PipedInputStream in) {
This.in = in;
}
Realize reading this thread
public void Run () {
try {
byte[] buf = new byte[1024];
int temp = 0;
Looping read
Read is a blocking method that needs to throw an exception
The code for the print stream is also added here
Because if the data is not read, then the printed code is not valid
while (temp = In.read (BUF))!=-1) {
String str = new string (buf,0,temp);
System.out.println (str);
}
catch (IOException e) {
In fact, this should throw a custom exception
I haven't figured it out yet.
E.printstacktrace ();
finally {
try {
I've thrown the fire, it's just to remind myself that it's important
In.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
}
}
Here to implement a write class
Class Write implements Runnable {
Private PipedOutputStream out;
To circulate pipe input in
Public Write (PipedOutputStream out) {
This.out = out;
}
public void Run () {
try {
Here, start writing the data.
Out.write ("Pipe Output". GetBytes ());
catch (IOException e) {
E.printstacktrace ();
finally {
try {
Should actually be able to write this off to the try inside.
But it's weird, it's not logical.
Out.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
}
}
public class Demo {
public static void Main (string[] args) {
PipedInputStream in = new PipedInputStream ();
PipedOutputStream out = new PipedOutputStream ();
try {
Connection pipe
In.connect (out);
Creating objects, opening threads
It's also put in the Try...catch.
Because if there is no chain to take over the road, the following operation meaningless
Read R = new read (in);
Write w = new write (out);
Put an object that has implemented the Run method into a thread to execute
New Thread (R). Start ();
New Thread (W). Start ();
catch (IOException e) {
E.printstacktrace ();
}
}
}

Well, the waste of so much effort, it printed such a sentence, abnormal abandoned to be very annoying, in order to pay attention to details ...


Pipe flow may be hard to understand, but it's not
We know that both the byte stream and the character stream require an array for streaming.
And the pipeline flow is directly concatenated two streams, while sending data, while receiving
However, two states of simultaneous communication, how to determine the consistency of sending and receiving?
Then, you need to use a thread, whether the receiver or the sender first executes the
Always creates a blocking state of a thread, waiting for the data from the other side to pass.
In general, the purpose of a pipe flow is to thread communication
In addition, there are Pipedreader and PipedWriter class, operating principles are the same, here will not repeat the
DataOutputStream and DataInputStream classes
Instance 4: Writes for basic data types

Copy Code code as follows:

Import Java.io.DataOutputStream;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
public class Demo {
public static void Main (string[] args) {
String Path = file.separator + "Home" + File.separator + "SIU" +
File.separator + "Work" + File.separator + "Demo.txt";
DataOutputStream d = null;
try {
Here you need to pass in an object of the OutputStream class
D = new DataOutputStream (new FileOutputStream (path));
Start writing basic data types
D.writeint (12);
D.writeboolean (TRUE);
D.writedouble (12.2223);
D.writechar (97);
Refresh Stream
D.flush ();
catch (IOException e) {
E.printstacktrace ();
finally {
try {
D.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
}
}

Here we do not intuitively understand the content, because it uses a byte-stream operation, rather than character streams
All we need to know is that this program has written the basic data type to the hard drive


Instance 5: Reading of basic data types

Copy Code code as follows:

Import Java.io.DataInputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
public class Demo {
public static void Main (string[] args) {
String Path = file.separator + "Home" + File.separator + "SIU" +
File.separator + "Work" + File.separator + "Demo.txt";
DataInputStream d = null;
try {
D = new DataInputStream (new FileInputStream (path));
Read basic data types in stored order
System.out.println (D.readint ());
System.out.println (D.readboolean ());
System.out.println (D.readdouble ());
System.out.println (D.readchar ());
catch (IOException e) {
E.printstacktrace ();
finally {
try {
D.close ();
catch (IOException e) {
E.printstacktrace ();
}
}
}
}

Note here that it is important to read in the write order, otherwise the data will be printed error

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.