Io of input and output operations in Java sum up of four

  • 2020-04-01 01:20:49
  • OfStack

I've covered the main operations of Java IO
In this section, we'll cover the rest of the Java IO story

The Serializable serialization
Example 1: serialization of an object
 
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 purpose of creating a reference to write to the stream is to play with the constructor of the ObjectOutputStream
FileOutputStream fos = null; 
ObjectOutputStream oos = null; 
try { 
fos = new FileOutputStream(path); 
oos = new ObjectOutputStream(fos); 
//Objects can be written here, as well as 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 persistence of an object to preserve its properties
In layman's terms, this is equivalent to pulling an object out of heap memory and putting it on the hard disk
Of course, if you're happy, you can serialize other things, including arrays, primitive data types, and so on
Look at the content. What the hell is this...

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010430231.png ">  
Example 2: deserialization of an object

 
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"; 
//Okay, so this is a little bit of code here, so throw an exception or something
//If that bothers you, toss it on the main method and use anonymous objects in the constructor
//What? Don't tell me you don't know anonymous
FileInputStream fis = null; 
ObjectInputStream ois = null; 
try { 
fis = new FileInputStream(path); 
ois = new ObjectInputStream(fis); 
//This is actually an Object class Object
//Because we know it's a Person class object
//So, it was transformed downward in place
Person p = (Person)ois.readObject(); 
System.out.println(p); 
//Throw dead you, vexed vexed vexed ~!!
} catch (IOException e) { 
e.printStackTrace(); 
} catch (ClassNotFoundException e) { 
e.printStackTrace(); 
} finally { 
try { 
//Still remember to turn off the riff
ois.close(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
} 
} 

You see, we store an object on a hard drive for later use
Now that you need it, take it out

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010430232.png ">
The pipe flow
Instance 3: thread communication

 
import java.io.IOException; 
import java.io.PipedInputStream; 
import java.io.PipedOutputStream; 
//Implement the Runnable interface to implement a read thread
class Read implements Runnable { 
private PipedInputStream in; 
//Pass the pipeline that needs to be read into the constructor
public Read(PipedInputStream in) { 
this.in = in; 
} 
//Implement read this thread
public void run() { 
try { 
byte[] buf = new byte[1024]; 
int temp = 0; 
//Loop reads
//Read is a blocking method that throws an exception
//Add the code for the print stream here
//Because if the data is not read, 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) { 
//Instead, a custom exception should be thrown
//I'm not sure yet
e.printStackTrace(); 
} finally { 
try { 
//I've thrown the fire, just to remind myself how important it is
in.close(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
} 
} 
//This implements a written class
class Write implements Runnable { 
private PipedOutputStream out; 
//Pass in pipe inputs
public Write(PipedOutputStream out) { 
this.out = out; 
} 
public void run() { 
try { 
//So let's start writing out the data
out.write(" Pipe the output ".getBytes()); 
} catch (IOException e) { 
e.printStackTrace(); 
} finally { 
try { 
//You should be able to write this closure method into the try above
//But this feels strange, the logic is not quite right
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); 
//Create an object and start a thread
//Again, put the try... The catch on the inside
//Because if there is no link pipe, the following operation is meaningless
Read r = new Read(in); 
Write w = new Write(out); 
//Put the object that has implemented the run method into the thread for execution
new Thread(r).start(); 
new Thread(w).start(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
} 

Ok, waste so big strength, printed such a sentence, abnormal discard come very annoying, in order to pay attention to detail...

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010430233.png ">
Pipeline flows may be hard to understand, but they aren't
We know that both byte streams and character streams require arrays to mediate the stream
A pipeline stream directly connects two streams, sending and receiving data
However, how can we determine the consistency of sending and receiving when both states are communicating at the same time
Then you need to use threads, either the receiver or the sender to execute first
It always blocks one thread, waiting for data from the other to pass
In general, the purpose of pipeline flow is for thread communication
In addition, there are the PipedReader and PipedWriter classes, both of which operate in the same way, which I won't go into here
DataOutputStream and a DataInputStream class
Example 4: write to the basic data type

 
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 the flow
d.flush(); 
} catch (IOException e) { 
e.printStackTrace(); 
} finally { 
try { 
d.close(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
} 
} 
} 

The content is not intuitive here because it operates as a byte stream, not as a stream of characters
We just need to know that the program has written the basic data types to the hard disk

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010430234.png ">
Example 5: reading of basic data types

 
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)); 
//Basic data types are read in storage 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(); 
} 
} 
} 
} 

The important thing to note here is that you must read in the write order, otherwise the data will be misprinted

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201301/2013011010430235.png ">


Related articles: