Location>code7788 >text

java basics - IO stream notes

Popularity:827 ℃/2024-09-19 11:17:07

610, Fundamentals of documentation

file stream

Input and output streams are both relative to the memory of a java program.

 

611, Creation of documents

 

Create a file under the D drive.

package ;

import ;
import ;
import ;

//Demonstration of file creation
public class FileCreate {
    public static void main(String[] args) {

    }

    @Test
    //Mode 1 new File(String pathname)
    public void create01() throws IOException {
        String filePath = "d:\\"; //  Path Write \\, or, /
        File file = new File(filePath);
        //Only when the createNewFile method is executed will the file actually be created on disk.
        ();
        ("File 1 created successfully.");
    }

    @Test
    //Mode 2 new File(File parent,String child)//Build from parent directory files + child paths
    public void create02() throws IOException {
        //The file object here, in a java program, is just an object.
        File parentFile = new File("d:\\");
        String fileName = "";
        File file = new File(parentFile, fileName);
        ();
        ("File 2 created successfully.");
    }

    @Test
    //way (of life) 3 new File(String parent,String child)//Build from parent directory + child path
    public void create03() throws IOException {
        String parentPath = "d:\\";
        String fileName = "";
        File file = new File(parentPath, fileName);
        ();
        ("Document 3 created successfully.");
    }
}

 

 

612, Access to documentation information

 

To write hello Han Soon-pyeong in UTF-8 encoding, an English character takes up 1 byte, and a Chinese character takes up 3 bytes.

package ;

import ;
import ;
import ;

//Demonstration of file creation
public class FileCreate {
    public static void main(String[] args) {

    }

    @Test
    public void info() {
        //Create the file object first
        File file = new File("d:\\");

        //Call the corresponding method to get the corresponding information
//getName、 getAbsolutePath、 getParent、 length、 exists、 isFile、 isDirectory

        ("File name = " + ());
        ("Absolute file path = " + ());
        ("File parent directory = " + ());
        ("File size in bytes = " + ());
        ("Does the file exist =" + ());
        ("Is not a file =" + ());
        ("Is not a catalog =" + ());
    }

}

 

613, Catalog Operations

 

After the 1st case code is written, the 2nd one just needs to change the filePath, so I won't write the 2nd case code.

In the third case, because D:\\demo\\\a is a multi-level directory and mkdir can only create one level of directory, the creation fails with mkdir.

package ;

import ;
import ;
import ;

//Demonstration of file creation
public class FileCreate {
    public static void main(String[] args) {

    }

    @Test
    public void m1() {
        String filePath = "d:\\";
        File file = new File(filePath);
        if (()) {
            //The delete() method is called to delete a file, and the return value determines whether the delete operation was successful.
            if(()) {
                (filePath + "Deleted successfully");
            } else {
                (filePath + "Deletion failed");
            }
        } else {
            ("The file does not exist...");
        }
    }

    @Test
    public void m2() {
        String directoryPath = "D:\\demo\\a";
        File file = new File(directoryPath);
        if(()) {
            (directoryPath + "exists");
        } else {
            if(()) {
                (directoryPath + "Created successfully");
            } else {
                (directoryPath + "Failed to create");
            }
        }
    }

}

 

614, IO Flow Principles and Classification

1, Principle:

 

2, Classification of streams

615,FileInputStream

 

Let's create a file on the D drive with the contents of hello,world.

package ;

import ;
import ;
import ;
import ;
import ;

//Demonstrate the use of FileInputStream (byte input stream File--> program)
public class FileInputStream_ {
    public static void main(String[] args) {

    }

    /*
     * Demonstrate reading a file...
     * Reading single bytes is inefficient.
     * -> Optimization: use read(byte[] b), see in readFile02 function
*/
    @Test
    public void readFile01() throws IOException {
        String filePath = "d:\\";
        int readData = 0;

        //Create a FileInputStream object for reading files.
        FileInputStream fileInputStream = new FileInputStream(filePath);

        //read(): read one byte of data from this input stream. This method will block if no input is available. The return value is of type int
//If -1 is returned, the reading is complete
        while ((readData = ()) != -1) {
            ((char)readData);//Convert int to char to display
        }

        //Closing the file stream, freeing up resources
        ();
    }

    /*
     * :: Use read(byte[] b) to read a file, for greater efficiency
*/
    @Test
    public void readFile02() throws IOException {
        String filePath = "d:\\";

        //byte array
        byte[] buf = new byte[8];//Reads 8 bytes at a time

        int readLenth = 0;
        //Create a FileInputStream object for reading files.
        FileInputStream fileInputStream = new FileInputStream(filePath);

        //read(byte[] b): reads up to bytes of data from this input stream into a byte array. This method will block until some input is available.
//If -1 is returned, the reading is complete
//If the read is normal, return the number of bytes actually read.
        while ((readLenth = (buf)) != -1) {
            (new String(buf, 0, readLenth));//Convert the return value of each byte array into a String for display
        }

        //Closing the file stream, freeing up resources
        ();
    }
}

 

616,FileOutputStream

FileOutputStream is a subclass of OutputStream, the byte output stream.

Files are created automatically without the use of mkdir().

package ;

import ;

import .*;

//Demonstrate the use of FileInputStream (byte input stream File--> program)
public class FileOutputStream_ {
    public static void main(String[] args) {

    }

    /*
     * Demonstrates writing data to a file using FileOutputStream, * creating the file if it does not exist.
     * Creates the file if it does not exist.
*/
    @Test
    public void writeFile() throws IOException {
        String filePath = "d:\\";

        //Create FileOutputStream object
//1. new FileOutputStream(filePath) creation method, when the content is written, will overwrite the original content
//2. new FileOutputStream(filePath, true) Creates a way to append to the back of the file when writing content.
        FileOutputStream fileOutputStream = new FileOutputStream(filePath, true);

        //Write a byte from memory to d:\\\
//        ('H');

        //Write string from memory to d:\\\
//() can take a string-> byte-array
        String str = " hello,world";
//        (());

        //write(byte[] b, int off, int len) Writes len bytes from the specified byte array at offset off to this file output stream.
        String str1 = " hsp";
        ((), 0, ());

        ();
    }

}

 

 

617, Document copy

Some files are too large to read into memory at once, so it's reading partial data

 

package ;

import ;

import .*;

public class FileCopy_ {
    public static void main(String[] args) throws IOException {
        //Finish copying the file, copy d:\ picture\ copy d:\
//analysis of ideas
//1. Create an input stream of files to be read into the program.
//2. create the output stream of the file, will read the file data, write to the specified file
        String srcFilePath = "d:\\ Picture\\";
        String destFilePath = "d:\\";//Can't forget.

        FileInputStream fileInputStream = new FileInputStream(srcFilePath);
        FileOutputStream fileOutputStream = new FileOutputStream(destFilePath);

        //Define a byte array to improve the reading effect
        byte[] buf = new byte[1024];
        int readLength = 0;
        while((readLength = (buf)) != -1) {
            //Once it's read, it's written to the file via fileOutputStream.
//I.e., reading and writing at the same time.
            (buf, 0, readLength);//Be sure to use this method
        }
        ("Copy successful.");

        //Close the input and output streams, freeing up resources
        if (fileInputStream != null) {
            ();
        }
        if (fileOutputStream != null) {
            ();
        }
    }
}

 

618, Document Character Stream Description

Reader and Writer are character streams, FileReader and FileWriter are their subclasses.

 

 

619,FileReader

First, create a new one on the D drive and write the contents in it

package ;

import ;

import .*;


public class FileReader_ {
    public static void main(String[] args)  {

    }

    /*
     * :: Single character reading of documents
*/
    @Test
    public void readFile01() throws IOException {
        String filePath = "d:\\";
        int data = 0;

        //1. Create FileReader object
        FileReader fileReader = new FileReader(filePath);

        //Cyclic reading Use read, single character reading.
        while ((data = ()) != -1) {
            ((char) data);
        }
        if (fileReader != null) {
            ();
        }
    }

    /*
     * :: Character arrays to read files
*/
    @Test
    public void readFile02() throws IOException {
        String filePath = "d:\\";
        int readLength = 0;
        char[] buf = new char[8];

        //1. Create FileReader object
        FileReader fileReader = new FileReader(filePath);

        //Cyclic read Use read(buf), the return is the number of characters actually read.
//If -1 is returned, the end of the file is reached.
        while ((readLength = (buf)) != -1) {
            (new String(buf, 0, readLength));
        }
        if (fileReader != null) {
            ();
        }
    }
}

 

620,FileWriter

package ;

import ;

import .*;


public class FileWriter_ {
    public static void main(String[] args) throws IOException {
        String filePath = "d:\\";

        //Create a FileWriter object
        FileWriter fileWriter = new FileWriter(filePath);//The default is to overwrite the write


//write(int): write single character
        ('H');

        //write(char[]): write to the specified array.
        char[] chars = {'a', 'b', 'c'};
        (chars);

        //write(char[],off,len): write the specified part of the specified array
        ("Han Soon Ping Education".toCharArray(), 0, 3);

        //write (string) : write the entire string
        ("After the storm, there's always a rainbow.");

        //write(string,off,len): write the specified part of the string.
        ("Shanghai Tianjin", 0, 2));
        //In case of a large amount of data, a loop can be used.
//For FileWriter, you have to close the stream or flush it to actually write the data to the file.
        ();
    }
}

 

622, Processing Flow Design Pattern

 

Simulating the modifier design pattern, the code structure is shown below:

 

Reader_ class: When you call it later, use the dynamic binding mechanism to bind it to the corresponding subclass.

package ;

public abstract class Reader_ {//abstract class

    public void readFile() {}

    public void readString() {}
}

 

FileReader_ class code:

package ;

//nodal flow
public class FileReader_ extends Reader_{
    public void readFile() {
        ("Performing a read on the file...");
    }
}

 

StringReader_ class code:

package ;

//nodal flow
public class StringReader_ extends Reader_{
    public void readString() {
        ("Read string.");
    }
}

 

BufferedReader_ class code:

package ;

//Make into a processing stream/packaging stream
public class BufferedReader_ extends Reader_{
    private Reader_ reader_;//attribute is of type Reader_

//Receive Reader_ subclass object
    public BufferedReader_(Reader_ reader_) {
        this.reader_ = reader_;
    }

    //Make the method more flexible, read the file multiple times, or add buffered char[]
    public void readFiles(int num) {
        for(int i = 0; i < num; i++) {
            reader_.readFile();
        }
    }

    //Extend readString to batch process string data.
    public void readStrings(int num) {
        for (int i = 0; i < num; i++) {
            reader_.readString();
        }
    }
}

 

Test_ class code:

package ;

public class Test_ {
    public static void main(String[] args) {
        BufferedReader_ bufferedReader = new BufferedReader_(new FileReader_());
        (3);

        //This time you want to read the string multiple times with BufferedReader_.
        BufferedReader_ bufferedReader1 = new BufferedReader_(new StringReader_());
        (3);

    }
}

Run results:

 

623,BufferedReader

 

package ;

import ;
import ;
import ;
import ;

//Demonstrates bufferedReader usage
public class BufferedReader_ {
    public static void main(String[] args) throws IOException {
        String filePath = "d:\\";

        //Create bufferedReader
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));

        //retrieve
        String line;//Reads by line, efficiently
//clarification
//1. () is to read the file line by line
//2. When null is returned, it means that the file has been read.
        while ((line = ()) != null) {
            (line);
        }

        //To close the stream, note that only the BufferedReader needs to be closed, because the underlying node stream FileReader is automatically closed.
        ();
    }
}

 

624,BufferedWriter

 

package ;

import .*;

//Demonstrates the use of BufferedWriter
public class BufferedWriter_ {
    public static void main(String[] args) throws IOException {
        String filePath = "d:\\";

        //Creating a bufferedWriter//Description.
//1. new FileWriter(filePath, true) means to write as append
//2. new FileWriter(filePath) , means to write as an overwrite
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));

        ("Hello, Han Soon-pyeong Education");
        ();//Insert a system-related newline
        ("Hello, Han Soon-pyeong Education"));
        ();

        //Explanation: To close the outer stream, pass in new FileWriter(filePath) and it will be closed at the bottom.
        ();
    }
}

 

625, Buffered Copy

package ;

import .*;

public class BufferedCopy_ {
    public static void main(String[] args) throws IOException {
        //1. BufferedReader and BufferedWriter are installed character operations.
//2. Do not manipulate binary files [sound, video, doc, pdf], as this may cause file corruption.
        String srcFilePath = "d:\\";
        String destFilePath = "d:\\";
        BufferedReader bufferedReader = new BufferedReader(new FileReader(srcFilePath));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(destFilePath));
        String line;

        //Description: readLine reads a line, but without line feeds.
        while ((line = ()) != null) {
            //Every time you read a line, you write
            (line);
            //Insert a newline
            ();
        }
        ("Copy successful.");

        //Stop the flow.
        if(bufferedReader != null) {
            ();
        }
        if(bufferedWriter != null) {
            ();
        }
    }
}

 

627, byte processing stream copy file

 

 

package ;

import .*;

/*
    * Demonstrates the use of BufferedOutputStream and BufferedInputStream.
    * BufferedOutputStream and BufferedInputStream to copy a binary file.
    * Think: Can a byte stream manipulate a binary file, but can it manipulate a text file? Of course you can.
*/
public class BufferedCopy_ {
    public static void main(String[] args) throws IOException {
        //1. BufferedReader and BufferedWriter are installed character operations.
//2. Do not manipulate binary files [sound, video, doc, pdf], as this may cause file corruption.
        String srcFilePath = "d:\\ Picture\\";
        String destFilePath = "d:\\";

        //establish BufferedOutputStream boyfriend BufferedInputStream boyfriend
        //Because FileInputStream is a subclass of InputStream.
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFilePath));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destFilePath));

        //Cyclically reads the file and writes it to destFilePath.
        byte[] buff = new byte[1024];
        int readLen = 0;

        //When -1 is returned, the file is read.
        while ((readLen = (buff)) != -1) {
            (buff, 0, readLen);
        }
        ("Copy successful.");

        //Close the stream, just close the outer processing stream, the bottom will close the node stream.
        if(bufferedInputStream != null) {
            ();
        }
        if(bufferedOutputStream != null) {
            ();
        }
    }
}

 

629,ObjectOutputStream

 

Dog Class

package ;

import ;

public class Dog implements Serializable {
    private String name;
    private int age;
    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public String getName(){
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

 

ObjectOutputStream_  resemble:
package ;

import ;
import ;

//Demonstrates the use of ObjectOutputStream to serialize data.
public class ObjectOutputStream_ {
    public static void main(String[] args) throws Exception {
        //After serialization, the saved file format is not plain text, but follows its formatting
        String filePath = "d:\\";

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
        //Serialize data to d:\
        (100);//int -> Integer (Integer implements Serializable)
        (true);//boolean -> Boolean (Boolean implements Serializable)
        ('a');//char -> Character (Character implements Serializable)
        (9.6);//double -> Double (Double implements Serializable)
        ("Top of the Year of the Rabbit").//String (String implements Serializable)
//Save a dog object
        (new Dog("Wanted", 10)));
        ();
        ("Data saved (serialized form) ---");
    }
}

 

630,ObjectInputStream

 

When you write the code for this section, you can't delete the previous code, you have to keep it, create a new class and write the code for this section.

 

ObjectInputStream_ class:
package ;

import ;
import ;

//Demonstrate the use of ObjectInputStream to deserialize data.
public class ObjectInputStream_ {
    public static void main(String[] args) throws Exception{
        //Specify the files to deserialize
        String filePath = "d:\\";

        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));

        //The order of reading (deserialization) needs to be the same as the order of saving the data (serialization), otherwise an exception will occur
        (());
        (());
        (());
        (());
        (());

        //The compile type of dog is Object and the run type of dog is Dog.
        Object dog = ();
        ("Type of operation:" + ());
        ("Dog information = " + dog);//Bottom Object -> Dog

//Important details
//1. If we wish to call Dog's methods, we need to transform downwards
//2. We need to put the definition of the Dog class in a location where it can be referenced.
        Dog dog2 = (Dog) dog;
        (());//Output only high wood

//To close the stream, just close the outer stream, the bottom will close it automatically
        ();
    }
}

 

 

632, standard input and output streams

 

package ;

import ;
import ;

public class InputAndOutput {
    public static void main(String[] args) {
        //System resemble (used form a nominal expression) public final static InputStream in = null;
        // Compile Type InputStream
// Run Type BufferedInputStream
// Indicates a standard input Keyboard
        (());
        ("Hello, Han Soon-pyeong Education.");
        (());

        //1, final static PrintStream out = null;
        //2, compiled type PrintStream
//3, run type PrintStream
//4 for standard output Display
        Scanner scanner = new Scanner();
        ("Input content");
        String next = ();
        ("next=" + next);
    }
}

 

633, garbled code leads to conversion stream

package ;

import ;
import ;
import ;
import ;
import ;
import ;

public class CodeQuestion {
    public static void main(String[] args) throws IOException {
        //Read d:\\\ file to program
//Thoughts:
//1, create character input stream BufferedReader [processing stream]
//2, using BufferedReader object to read
//3, by default, read the file according to utf-8 encoding
        String filePath = "d:\\";
        BufferedReader br = new BufferedReader(new FileReader(filePath));

        String s = ();
        ("Read:" + s);
        ();
    }
}

 

This is UTF-8 encoded to output results:

This is the output result of changing to ANSI, that is, gbk code:

 

634,InputStreamReader

 

package ;

import .*;

//Demonstrate the use of InputStreamReader to convert streams to solve the problem of Chinese garbled code.
//* :: Converts a byte stream FileInputStream to a character stream InputStreamReader, specifying the encoding gbk/utf-8.
public class InputStreamReader_ {
    public static void main(String[] args) throws IOException {
        String filePath = "d:\\";

        //1. convert FileInputStream to InputStreamReader
//2. Specify the encoding gbk
        InputStreamReader isr = new InputStreamReader(new FileInputStream(filePath), "gbk");

        //3. pass InputStreamReader into BufferedReader
//Combine 2 and 3
        BufferedReader br = new BufferedReader(isr);
        String s = ();
        
        ("Read content = " + s);

        //5. Closure of the outer stream
        ();

    }
}

 

 635,OutputStreamWriter

 

package ;

import .*;
import ;

//Demonstrates the use of OutputStreamWriter using the
//Converts a byte stream FileOutputStream to a character stream OutputStreamWriter.
//Specify the encoding to be processed gbk/utf-8/utf8
public class OutputStreamWriter_ {
    public static void main(String[] args) throws IOException {
        String filePath = "d:\\";
        String charSet = "utf-8";
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filePath), charSet);
        ("Hi, Han Soon-pyeong.");
        ();

        ("Saved the file successfully according to " + charSet + " ~");
    }
}

 

 636,PrintStream, PrintStream

PrintStrean Byte Print Stream

 

package ;

import .*;

// Demo PrintStream (Byte PrintStream/OutputStream)
public class PrintStream_ {
    public static void main(String[] args) throws IOException {

        PrintStream out = ;
        //By default, PrintStream outputs data to the standard output, i.e., the monitor.
        ("john, hello");
        //Since print uses write as the underlying language, we can call write directly to print/output.
        ("Hello, Han Soon-pyeong").getBytes());
        ();

        //We can go ahead and modify the location/device of the print stream output
//1. Output modified to "e:\\"
//2. "Hello, Han Soon-pyeong Education" will be output to e:\
        (new PrintStream("d:\\"));
        ("Hello, Han Soon-pyeong.");
    }
}

 

package ;

import .*;

//Demonstrate PrintWriter usage
public class PrintStream_ {
    public static void main(String[] args) throws IOException {

        //PrintWriter printWriter = new PrintWriter()
        PrintWriter printWriter = new PrintWriter(new FileWriter("d:\\"));
        ("Hi, Beijing.");
        //flush + closes the stream before writing the data to the file...
        ();
    }
}

 

638, Configuration file leading to Properties

 

Using traditional methods

First, create a properties file under src called

ip=192.168.100.100
user=root
pwd=12345

 

Master file code:

Note that there is no colon in this path.

package .properties_;

import ;
import ;
import ;
import ;

public class Properties01 {
    public static void main(String[] args) throws IOException {

        //Read the file and get ip, user and pwd.
        BufferedReader br = new BufferedReader(new FileReader("src\\"));
        String line = "";
        while ((line = ()) != null) { //Cyclic reading
            String[] split = ("=");
            //(split[0] + "The value is:" + split[1]);

//If we ask for the specified ip value
            if("ip".equals(split[0])) {
                (split[0] + "The value is: " + split[1]);
            }
        }
        ();
    }
}

 

639, Properties read file

package .properties_;

import ;
import ;
import ;
import ;
import ;

public class Properties01 {
    public static void main(String[] args) throws IOException {
        //Using the Properties Class to Read Files
//1. Create the Properties object
        Properties properties = new Properties();
        //2. Load the specified configuration file
        (new FileReader("src\\"));
        //3. Put k-v on the display console
        ();
        //4. Get the corresponding value based on the key
        String user = ("user");
        String pwd = ("pwd");
        ("Username=" + user);
        ("The password is =" + pwd);
    }
}

 

640, Properties modification file

package .properties_;

import .*;
import ;

public class Properties01 {
    public static void main(String[] args) throws IOException {
        //Use the Properties class to create a configuration file and modify its contents.
        Properties properties = new Properties();
        ("charset", "utf8");//Note that when you save, it is the Chinese unicode value.
        ("user", "Tom"));
        ("pwd", "abc111");
        //Just store the k-v in a file
        (new FileOutputStream("src\\"), null);
        ("Saving configuration file successful~");
    }
}

 

641, homework for this chapter 01

 

package ;

import ;
import ;
import ;
import ;

public class Homework01 {
    public static void main(String[] args) throws IOException {
        String directoryPath = "d:\\mytemp";
        File file = new File(directoryPath);
        if (!()) {
            //Create a catalog
            if(()) {
                ("Create " + directoryPath + " Created successfully");
            } else {
                ("Create " + directoryPath + " failed to create");
            }
        }

        String filePath = directoryPath + "\\";  // d:\mytemp\
        file = new File(filePath);
        if (!()) {
            //Creating Documents
            if (()) {
                (filePath + " Created successfully~");

                //If the file exists, we use the BufferedWriter character input stream to write the content
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
                ("hello, world~~ Han Soon-pyeong Education");
                ();

            } else {
                (filePath + " Failed to create ");
            }
        } else {
            //If the file already exists, give a prompt
            (filePath + " already exists, duplicate creation if not..."));
        }
    }
}

 

642, Chapter Homework 02

 

package ;

import .*;

public class Homework02 {
    public static void main(String[] args) throws IOException {
        String filePath = "d:\\";
        BufferedReader br = null;
        String line = "";
        int lineNum = 0;
        br = new BufferedReader(new FileReader(filePath));
        while ((line = ()) != null) {//Cyclic reading
            (++lineNum + " " + line);
        }
        if (br != null) {
            ();
        }
    }
}

 

643, Chapter Homework 03

 

package ;

import ;

import .*;
import ;

public class Homework03 {
    public static void main(String[] args) throws IOException {
        String filePath = "src\\";
        Properties properties = new Properties();
        (new FileReader(filePath));
        String name = ("name");
        int age = (("age"));// String -> int
        String color = ("color");

        Dog dog = new Dog(name, age, color);
        ("====dog object information ====");
        (dog);

        //Serialize the created Dog object, to the file d.
        String serFilePath = "d:\\";
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(serFilePath));
        (dog);

        //Stop the flow.
        ();
        ("Dog object, serialization complete...");
    }

    @Test
    public void m1() throws IOException, ClassNotFoundException {
        String serFilePath = "d:\\";
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(serFilePath));
        Dog dog = (Dog)();

        ("==== after deserialization dog====");
        (dog);
        
        ();
    }
}

class Dog implements Serializable{
    private String name;
    private int age;
    private String color;

    public Dog(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }
}

 

The result after running the m1 method: