Input / output stream in java (2)
1, Commonly used byte input and byte output stream classes
1. Byte output stream - top class OutputStream
Definition - public abstract class OutputStream - cannot be new
Byte output stream ----- FileOutputStream class
Definitions - public class fileoutputstream extensions OutputStream
Construction method
1.FileOutputStream(File file) creates a non appendable byte output stream through the File object.
2. Fileoutputstream (File, Boolean append) creates a whether to trace through the File object
Added byte output stream.
3.FileOutputStream(String name) creates a non appendable byte output stream through a String object.
4.FileOutputStream(String name, boolean append) creates a byte output stream whether to append or not through a String object
For example:
package com.shuchu.gouzao.java; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; public class Test { public static void main(String[] args) throws Exception { //1.FileOutputStream(File file) creates a non appendable byte output stream through the File object. String path = "E:"+File.separator+"test"+"name.txt"; File le = new File(path); FileOutputStream out = new FileOutputStream(le); OutputStream outt = new FileOutputStream(le); //2. Fileoutputstream (File, Boolean append) creates a whether to trace through the File object //Added byte output stream. FileOutputStream out2 = new FileOutputStream(le,true); //3.FileOutputStream(String name) creates a non appendable byte output stream through a String object. FileOutputStream out3 = new FileOutputStream(path); OutputStream out4 = new FileOutputStream(path); //4.FileOutputStream(String name, boolean append) creates a byte output stream whether to append or not through a String object FileOutputStream out5 = new FileOutputStream(path,true); } }
Example method
1.void write(byte[] b) writes b.length bytes from the specified byte array to the file output stream.
2.void write(byte[] b, int off, int len) writes len bytes to the file output stream from the specified byte array at offset off.
3.void write(int b) writes the specified bytes to this file output stream.
4.void close() closes the file output stream and frees any system resources associated with the stream.
For example:
package com.shuchu.fangfa.java; import java.io.File; import java.io.FileOutputStream; public class Test { public static void main(String[] args) throws Exception{ //Output the data to a file by writing out the output stream //Prepare the data to be written out String hl = "Hello,World"; //Create byte output stream object String wj = "E:"+File.separator+"text"+File.separator+"name.txt"; File le = new File(wj); FileOutputStream out = new FileOutputStream(le,true); /* //1.void write(byte[] b) Writes b.length bytes from the specified byte array to this file output stream. out.write(hl.getBytes()); //4.void close() Close this file output stream and free up any system resources associated with this stream. out.close(); */ /* //2.void write(byte[] b, int off, int len) Writes len bytes to this file output stream from the specified byte array at offset off. byte b[] = hl.getBytes(); out.write(b,6,5); //Close flow out.close(); */ //3.void write(int b) writes the specified bytes to this file output stream. [write out only one byte at a time] byte arry[] = hl.getBytes(); for(byte te:arry) { out.write(te); } //Close flow out.close(); } }
Execution result:
Note: you can only write out the basic data type of the above stream, but not the bytes
Public class dataoutputstream extensions filteroutputstream implements dataoutput class
The data output stream enables applications to write raw Java data types to the output stream in a portable manner
Construction method
1.DataOutputStream(OutputStream out) creates a new data output stream to write data to the specified underlying output stream.
For example:
package com.DataOutputStream.gouzao.java; import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; public class Test { public static void main(String[] args) throws Exception { //1.DataOutputStream(OutputStream out) creates a new data output stream to write data to the specified underlying output stream. String path = "E:"+File.separator+"text"+File.separator+"id.txt"; File le = new File(path); FileOutputStream out = new FileOutputStream(le); OutputStream outt = new FileOutputStream(le); DataOutputStream out2 = new DataOutputStream(out); } }
Example method:
1.void writeBoolean(boolean v) writes boolean to the underlying output stream as a 1-byte value.
2.void writeByte(int v) writes byte as a 1-byte value to the underlying output stream.
3.void writeChar(int v) writes char to the underlying output stream as a 2-byte value, with the high byte taking precedence.
4.void The writeDouble(double v) Double parameter is passed to the conversion long. Use the doubleToLongBits method in the class Double, and then write the long value to the basic output stream as the number of 8 bytes, high bytes.
5.void writeFloat(float v) the conversion int of the Float parameter uses the floatToIntBits method in the class Float, and then writes the int value to the basic output stream as a 4-byte quantity, high byte.
6.void writeInt(int v) writes the underlying output stream to int as a four byte, high-order byte.
7.void writeLong(long v) writes long to the underlying output stream, which is 8 bytes, with the high byte first.
8.void writeShort(int v) writes short to the underlying output stream as two bytes, with the high byte taking precedence.
9.void writeUTF(String str) writes a string to the underlying output stream in a machine independent manner using modified UTF-8 encoding.
10.void flush() refreshes this data output stream.
11.void close() closes the output stream and frees any system resources associated with the stream.
For example:
package com.DataOutputStream.fangfa.java; import java.io.DataOutputStream; import java.io.File; import java.io.FileOutputStream; public class Test { public static void main(String[] args) throws Exception { //DataOutputStream(OutputStream out) creates a new data output stream to write data to the specified underlying output stream. String path = "E:"+File.separator+"chen"+File.separator+"id.txt"; File le = new File(path); FileOutputStream out = new FileOutputStream(le); DataOutputStream outt = new DataOutputStream(out); //1.void writeBoolean(boolean v) writes boolean to the underlying output stream as a 1-byte value. outt.writeBoolean(true); //3.void writeChar(int v) writes char to the underlying output stream as a 2-byte value, with the high byte taking precedence. outt.writeChar('\t'); //2.void writeByte(int v) writes byte as a 1-byte value to the underlying output stream. outt.writeByte(30); outt.writeChar('\t'); //4.void The writeDouble(double v) Double parameter is passed to the conversion long. Use the doubleToLongBits method in the class Double, and then write the long value to the basic output stream as the number of 8 bytes, high bytes. outt.writeDouble(123.1); outt.writeChar('\t'); //5.void writeFloat(float v) the conversion int of the Float parameter uses the floatToIntBits method in the class Float, and then writes the int value to the basic output stream as a 4-byte quantity, high byte. outt.writeFloat(12.5f); outt.writeChar('\t'); //6.void writeInt(int v) writes the underlying output stream to int as a four byte, high-order byte. outt.writeInt(200); outt.writeChar('\t'); //7.void writeLong(long v) writes long to the underlying output stream, which is 8 bytes, with the high byte first. outt.writeLong(80000); outt.writeChar('\t'); //8.void writeShort(int v) writes short to the underlying output stream as two bytes, with the high byte taking precedence. outt.writeShort(30); outt.writeChar('\t'); //9.void writeUTF(String str) writes a string to the underlying output stream in a machine independent manner using modified UTF-8 encoding. outt.writeUTF("lisi"); outt.writeChar('\t'); //10.void flush() refreshes this data output stream. outt.flush(); //11.void close() closes the output stream and frees any system resources associated with the stream. outt.close(); out.close(); } }
2. Byte output stream - top class InputStream
public abstract class InputStream - cannot new
Public class FileInputStream extensions InputStream class
Construction method
1.FileInputStream(File file) creates a byte input stream through the File object
2.FileInputStream(String name) creates a byte input stream through a String object
For example:
package com.FileInputStream.gouzao.java; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; public class Test { public static void main(String[] args) throws Exception { //1.FileInputStream(File file) creates a byte input stream through the File object String path = "E:"+File.separator+"text"+"name.txt"; File le = new File(path); FileInputStream in = new FileInputStream(le); InputStream intt = new FileInputStream(le); //2.FileInputStream(String name) creates a byte input stream through a String object FileInputStream in2 = new FileInputStream(path); InputStream inttt = new FileInputStream(path); } }
Example method
1.int read() reads one byte of data from the input stream.
Return value: the int type of the specific byte data read. If it reaches the end of the file, it returns - 1
2.int read(byte[] b) reads up to b.length bytes of data from the input stream, which is a byte array.
Return value: the total number of bytes read. If it reaches the end of the file, it returns - 1
3.void close() closes the file input stream and frees any system resources associated with the stream.
For example:
package com.FileInputStream.fangfa.java; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class Test { public static void main(String[] args) throws Exception { //Read name Txt into our program String path ="E:"+File.separator+"text"+File.separator+"name.txt"; //Create the corresponding field object according to the file path File le = new File(path); //Create byte input stream object FileInputStream in = new FileInputStream(le); /* //1.int read() Read one byte of data from the input stream. Return value: the int type of the specific byte data read. If it reaches the end of the file, it returns - 1 byte arry[] = new byte[(int)le.length()]; int len = in.read(arry); //Close flow in.close(); String name = new String(arry,0,len); System.out.println(name); */ //2.int read(byte[] b) reads up to b.length bytes of data from the input stream, which is a byte array. Return value: the total number of bytes read. If it reaches the end of the file, it returns - 1 byte arry2[] = new byte[(int)le.length()]; //Save the data value read each time int data = 0; //Array subscript int len2 = 0; while((data = in.read())!=-1) { arry2[len2]=(byte)data; len2++; } //Close flow in.close(); String name2 = new String(arry2,0,len2); System.out.println(name2); } }
Public class datainputstream extensions filterinputstream implements datainputstream class
Construction method
1.DataInputStream(InputStream in) creates a DataInputStream that uses the specified underlying InputStream.
For example:
package com.DataInputStream.gouzao.java; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; public class Test { public static void main(String[] args) throws Exception { //1.DataInputStream(InputStream in) creates a DataInputStream that uses the specified underlying InputStream. String path = "E:"+File.separator+"chen"+"id.txt"; File le = new File(path); FileInputStream in = new FileInputStream(le); DataInputStream inn = new DataInputStream(in); } }
Example method
1.void readBoolean(boolean v) reads boolean into the underlying output stream as a 1-byte value.
2.void readByte(int v) reads byte as a 1-byte value into the underlying output stream.
3. Void readchar (int V) reads char into the underlying output stream as a 2-byte value, with high byte taking precedence.
4.void The readDouble(double v) Double parameter is passed to the conversion long. Use the doubleToLongBits method in the class Double, and then read the long value into the basic output stream as the number of 8 bytes, high bytes.
5.void readFloat(float v) the conversion int of Float parameter uses the floatToIntBits method in the class Float, and then reads the int value into the basic output stream as a 4-byte quantity, high byte.
6.void readInt(int v) reads the underlying output stream into int as four bytes and high-order bytes.
7.void readLong(long v) reads long into the underlying output stream, which is 8 bytes, with the high byte first.
8.void readShort(int v) reads short into the underlying output stream as two bytes, and the high byte takes precedence.
9.void readUTF(String str) reads strings into the underlying output stream in a machine independent manner using modified UTF-8 encoding.
10.void close() closes the output stream and frees any system resources associated with the stream.
For example:
package com.DataInputStream.fangfa.java; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; public class Test { public static void main(String[] args) throws Exception { //DataInputStream(InputStream in) creates a DataInputStream that uses the specified underlying InputStream. String path = "E:"+File.separator+"chen"+File.separator+"id.txt"; File le = new File(path); FileInputStream in = new FileInputStream(le); DataInputStream inn = new DataInputStream(in); //1.void readBoolean(boolean v) reads boolean into the underlying output stream as a 1-byte value. boolean vau = inn.readBoolean(); //3. Void readchar (int V) reads char into the underlying output stream as a 2-byte value, with high byte taking precedence. char zf = inn.readChar(); //2.void readByte(int v) reads byte as a 1-byte value into the underlying output stream. byte zj = inn.readByte(); char zf1 = inn.readChar(); //4.void The readDouble(double v) Double parameter is passed to the conversion long. Use the doubleToLongBits method in the class Double, and then read the long value into the basic output stream as the number of 8 bytes, high bytes. double v = inn.readDouble(); char zf2 = inn.readChar(); //5.void readFloat(float v) the conversion int of Float parameter uses the floatToIntBits method in the class Float, and then reads the int value into the basic output stream as a 4-byte quantity, high byte. float dan = inn.readFloat(); char zf3 = inn.readChar(); //6.void readInt(int v) reads the underlying output stream into int as four bytes and high-order bytes. int num = inn.readInt(); char zf4 = inn.readChar(); //7.void readLong(long v) reads long into the underlying output stream, which is 8 bytes, with the high byte first. long chang = inn.readLong(); char zf5 = inn.readChar(); //8.void readShort(int v) reads short into the underlying output stream as two bytes, and the high byte takes precedence. short duan = inn.readShort(); char zf6 = inn.readChar(); //9.void readUTF(String str) reads strings into the underlying output stream in a machine independent manner using modified UTF-8 encoding. String str = inn.readUTF(); char zf7 = inn.readChar(); //10.void close() closes the output stream and frees any system resources associated with the stream. inn.close(); in.close(); System.out.println(vau); System.out.println(zf); System.out.println(zj); System.out.println(zf1); System.out.println(v); System.out.println(zf2); System.out.println(dan); System.out.println(zf3); System.out.println(num); System.out.println(zf4); System.out.println(chang); System.out.println(zf5); System.out.println(duan); System.out.println(zf6); System.out.println(str); System.out.println(zf7); } }
Execution result:
2, What is serialization? How to implement serialization?
Because the java objects we operate on may need to be passed between multiple computers.
Serialization ---- the process of converting a java object into binary stream data.
Deserialization - the process of converting binary stream data into a java object.
How to implement serialization?
1. Implement a Serializable interface for the generated class of the serialized java object
public interface Serializable
Special - there is no method in this interface.
Class serialization is implemented by Java io. The class of the serializable interface is enabled. Classes that do not implement this interface will not serialize or deserialize any state. All subtypes of a serializable class are serializable.
2. Provide writeObject(Object obj) of ObjectOutputStream class through java
Construction method of ObjectOutputStream
ObjectOutputStream(OutputStream out) creates an ObjectOutputStream that writes to the specified OutputStream.
Example method
void writeObject(Object obj) writes the specified object to ObjectOutputStream.
For example:
package com.ObjectOutputStream.java; import java.io.Serializable; public class Student implements Serializable{ public void shiLi() { System.out.println("This is Student Example method of"); } }
Write Student object to Notepad
package com.ObjectOutputStream.java; import java.io.File; import java.io.FileOutputStream; import java.io.ObjectOutputStream; public class Test { public static void main(String[] args) throws Exception { Student stu = new Student(); //ObjectOutputStream(OutputStream out) creates an ObjectOutputStream that writes to the specified OutputStream. String path = "E:"+File.separator+"lianxi"+File.separator+"student.txt"; File le = new File(path); FileOutputStream out = new FileOutputStream(le); ObjectOutputStream outt = new ObjectOutputStream(out); //void writeObject(Object obj) writes the specified object to ObjectOutputStream. outt.writeObject(stu); outt.flush(); outt.close(); out.close(); } }
Execution result:
Serialize the above java object into the notepad file and deserialize it back into a usable java object. At this point, you need the object readObject () method of ObjectInputStream class to read the object.
ObjectInputStream class
Construction method
ObjectInputStream(InputStream in) creates an ObjectInputStream read from the specified InputStream.
Example method
Object readObject() reads an object from ObjectInputStream.
For example:
package com.ObjectInputStream.java; import java.io.File; import java.io.FileInputStream; import java.io.ObjectInputStream; import com.ObjectOutputStream.java.Student; public class Test { public static void main(String[] args) throws Exception { //ObjectInputStream(InputStream out) creates an ObjectInputStream read from the specified InputStream. String path = "E:"+File.separator+"lianxi"+File.separator+"student.txt"; File le = new File(path); FileInputStream in = new FileInputStream(le); ObjectInputStream inn = new ObjectInputStream(in); //Object readObject() reads an object from ObjectInputStream. Object o = inn.readObject(); inn.close(); in.close(); Student stu = (Student)o; stu.shiLi(); } }
Execution result:
io structure: