Conversion flow
To process the flow, you need to pass in the node flow
OutputStreamWriter
Byte output stream to character output stream
// Byte output stream FileOutputStream fos = new FileOutputStream("D:\\123.txt"); // Convert to character stream OutputStreamWriter osw = new OutputStreamWriter(fos); osw.write("xxxxxxxxxxxxx"); osw.flush(); // After osw is closed, fos will be closed automatically osw.close();
InputStreamReader
Byte input stream to character input stream
InputStreamReader(InputStream in)
Inputstreamreader (InputStream in, charset CS) / / specify the character set
Inputstreamreader (InputStream in, charsetdecoder DEC) / / specifies the character set decoder
void close()
String getEncoding()
int read()
int read(char[] char , int start , int length)
boolean ready()
Open the flow first and then close it
Buffer stream
BufferedReader
Character input buffer stream
Function: provide efficiency, save the input and output to the buffer first, and then write / write at one time
// Byte stream FileInputStream fis = new FileInputStream("./src/com/Test_01.java"); // Convert to character InputStreamReader isr = new InputStreamReader(fis); // Character buffer stream BufferedReader br = new BufferedReader(isr); ){ String temp = null; while ((temp=br.readLine()) != null) { System.out.println(temp); }
BufferedWriter
bw.write("anisa Sadik"); // Write line breaks bw.newLine(); bw.write("anisa Sadik"); // Brush cache bw.flush();
Print stream
The print stream points to the console by default
// Use the print stream in the system, and the default print console System.out.println("Hello"); PrintStream out = System.out; out.println("java....."); // Create your own print stream FileOutputStream fos = new FileOutputStream("D:/log.txt"); // Create print stream to facilitate output operation PrintStream ou1 = new PrintStream(fos); // Using the original byte output stream, the operation is cumbersome and the type is limited fos.write("Start execution...".getBytes()); // In the print stream, the operation is encapsulated to make the operation more convenient. In addition, there are many print method overloads, which support all types ou1.println("Start execution....."); ou1.println(true); ou1.println(1); ou1.println(1.2); // System provides a way to set the out print path System.setOut(ou1); // All the following print statements are printed to the specified file System.out.println("xxxxxxxxxxxxxxxxxx");
File copy
Input before output
Input and output directories cannot be consistent
// The default is overwrite write. When creating an output stream object, because it is overwrite write, the corresponding file will be cleared directly // FileOutputStream fos = new FileOutputStream("D:/log.txt"); // If it is an additional write, the corresponding file will not be emptied // However, if the file is copied, there will be an endless loop problem FileOutputStream fos = new FileOutputStream("D:/log.txt", true);
data stream
DataInputStream and DataOutputStream
The sequence of writing and reading is consistent
dos.writeByte(b); dos.writeInt(i); dos.writeLong(l); dos.writeChar(c); dos.writeBoolean(flag); dos.writeUTF("how are you"); System.out.println(dis.readByte()); System.out.println(dis.readInt()); System.out.println(dis.readLong()); System.out.println(dis.readChar()); System.out.println(dis.readBoolean()); System.out.println(dis.readUTF());
serialize
There are four ways to create objects:
1. new is used most
2. Reflection mechanism: objects can be created through strings
3. Deserialization
4. clone is not used and is replaced by serialization
Serialization stream is an implementation means of converting an object into a binary stream. Through serialization, the object can be persistently stored and transmitted
The serialized object must implement the Serializable interface, but there is no function in the interface, just a tag
Serialization is the process of persisting and storing objects in the hard disk
Deserialization is to load the objects in the hard disk into heap memory
Application scenario:
Serialization converts data objects into binary streams, which can achieve persistent storage and network transmission. Without serialization, data transmission and storage cannot be carried out
Data is transmitted through the network. The transmission from different machines needs to go through a process of serialization and deserialization
Object -- > serialization -- > data encryption processing -- > network transmission -- > data decryption processing -- > deserialization -- > object
// System.out.println(user); // Create serialized stream object ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/123.o")); // serialized objects oos.writeObject(user); // Brush cache oos.flush(); // close oos.close();
// Create deserialized stream object ObjectInputStream ois = new ObjectInputStream(new FileInputStream( "D:/123.o")); // Deserialize, read the Object and return the Object type User user = (User) ois.readObject(); // close resource ois.close();
The object to serialize must implement the Serializable interface
version control
If our class file changes after serialization
Each class has a UID to represent the current version. As long as there is a change, the UID will change with it
Therefore, as long as the class is changed or a new function is added, an error will be reported if it is deserialized again
Therefore, generally, we will specify a UID separately to ensure the consistency of versions. Generally, 1L is enough
Transient
If you don't want a variable to be serialized, you can use the transient modifier
Unnecessary data can not be serialized, so as to improve the efficiency of serialization and deserialization
private static final long serialVersionUID = 1L; public transient String name;
File class
common method
// Get file object // If the path is, an empty string equals/ current directory File file = new File("D:\\17 stage\\Courseware"); // Absolute position D: \ \ issue 17 \ \ courseware System.out.println(file.getAbsolutePath()); // Name courseware System.out.println(file.getName()); // Parent path D: \ phase 17 System.out.println(file.getParent()); // Whether it is directory true System.out.println(file.isDirectory()); // Is the file false System.out.println(file.isFile()); // The last modification time returns the number of milliseconds System.out.println(new Date(file.lastModified())); // Determine whether there is true System.out.println(file.exists()); file = null; File file1 = new File("D:\\com\\a\\b.docx"); // Delete, Successful return true System.out.println(file1.delete()); file1 = new File("D:\\com\\a\\b\\c"); // Judge whether it exists System.out.println(file1.exists()); // All directories are created recursively, but files are not created // mkdir can only create one subdirectory, If the parent directory does not exist, it will not be created file1.mkdirs(); file1 = new File("D:/com/a/a.txt"); // If the file is created successfully, it is true; otherwise, it is false file1.createNewFile(); // Change name file1.renameTo(new File("D:/com/a/hold.txt")); file1 = new File("D:/com/a"); // Get all sub file objects File[] subFiles = file1.listFiles(); for (File file2 : subFiles) { System.out.println(file2.getAbsolutePath()); }