Today we’ll talk about the —————– character stream for IO operations
Both Reader and Writer are abstract classes, so use their subclasses InputStreamReader and OutputStreamWriter
1.OutputStreamWriter
Inheritance relationships
Write data to a file using a character stream
Public class TestWrite {public static void main(String[] args) throws Exception {// Create a stream object OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("d:\\hello.txt".true)); // Prepare data. String s ="Good morning to 1208"; // Write data osw.write(s); // Close the resource osw.close(); }}Copy the code
2.InputStreamReader
Inheritance relationships
public class TestRead {
public static void main(String[] args) throws Exception {
InputStreamReader isr = new InputStreamReader(new FileInputStream("d:\\hello.txt"));
int r = 0;
while(1! = (r = isr.read())) { System.out.println((char)r); } isr.close(); }}Copy the code
3. Byte to character stream (encoding format)
4. Copy text files. Use character streams to copy non-text files
This approach is particularly inefficient
public class TestCopy {
public static void main(String[] args) throws Exception {
InputStreamReader isr = new InputStreamReader(new FileInputStream("d:\\j.png"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("c:\\j.png"));
int r = 0;
while(1! = (r = isr.read())) { osw.write(r); } osw.close(); isr.close(); }}Copy the code
Efficient copying of text files (important)
Sample code:
Public class TestCopy2 {public static void main(String[] args) throws Exception {InputStreamReader ISr = new InputStreamReader(new FileInputStream("d:\\hello.txt"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("c:\\hello.txt"));
char[] cbuf = new char[1024];
int len = 0;
while(1! = (len = isr.read(cbuf))) { osw.write(cbuf, 0, len); } osw.close(); isr.close(); }}Copy the code
5. Stream buffer
Using buffer streams to copy text files is also an internal character array, which also needs to be converted to each other when it is read and written. Therefore, it is less efficient than defining a character array directly.
The sample code
public class TestCopy3 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\hello.txt")));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:\\hello.txt")));
String line = null;
while(null != (line = br.readLine())) {
bw.write(line);
bw.newLine();//表示和系统相关的换行符
}
bw.close();
br.close();
}
}
Copy the code
6. Serialization and deserialization
Serialization: Save the object to hard disk using the writeObject method in ObjectOutputStream
ObjectOutputStream inheritance:
Deserialization: Read an object from the hard disk, using the readObject method in ObjectInputStream
ObjectInputStream inheritance relationship:
Before serializing an object, you must have the entity class implement the Serializable interface
The Serializable interface, which has neither properties nor methods, is called a signature interface
1. Serialize the object to memory
Public class TestWriteObject {public static void main(String[] args) throws Exception {// If you want a class to be serialized, We need to implement the Serializable interface. //Serializable interface has no attributes and no methods. We call this interface Dog."Prosperous wealth.", 2, "Black");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:\\dog.txt")); oos.writeObject(dog); /ere/ can only write one object at a time, and if you want to write multiple objects you need to put oos.close() in the collection; }}Copy the code
2. Deserialize objects from hard disk to memory
public class TestReadObject {
public static void main(String[] args) throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:\\dog.txt")); Dog dog = (Dog) ois.readObject(); System.out.println(dog); ois.close(); }}Copy the code
Note that if you change the structure of the corresponding class of a serialized object, you cannot deserialize it
When serializing, a value UID will be calculated according to the structure of the class. When deserializing, a value UID will be calculated. The two values will be compared
Manually generate the UID in front of the attributes of the entity class
private static final long serialVersionUID = 1L;
Copy the code
If you do not want some data to be deserialized, you can add the TRANSIENT keyword
private transient String color;
Copy the code
3. Serialize the collection to disk
public class TestWriteBook {
public static void main(String[] args) throws Exception {
ArrayList<Book> books = new ArrayList<Book>();
books.add(new Book("Java books 1"."Miss Zhang 1", 8001));
books.add(new Book("Java books 2"."Miss Zhang 2", 8002));
books.add(new Book("Java collection of 3"."Miss Zhang 3", 8003));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:\\books.txt"));
oos.writeObject(books);
oos.close();
}
}
public class TestReadBook {
public static void main(String[] args) throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:\\books.txt")); ArrayList<Book> books = (ArrayList<Book>) ois.readObject(); System.out.println(books); ois.close(); }}Copy the code
7. Use BufferedReader instead of Scanner
System. In A standard input stream in which keyboard data can be saved
System.out A standard print stream that outputs data from the stream to the screen
You can use BufferedReader instead of Scanner
Sample code:
public class TestRead4 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter your name");
String name = br.readLine();
System.out.println("name="+ name); br.close(); }}Copy the code
Io character stream is introduced so much, the other methods of character stream operation is not quite skilled small cute people, you can see the official API documentation
Remember to like + follow 👉 : my github address: github.com/Lmobject