Java Initial - Input and Output Streams

Keywords: Java

Use of File Classes

The file is: the file can be considered as a collection of related records or data put together.
In Java, you use the java.io.File class to manipulate files

public class FileDemo {
    public static void main(String[] args) {
        String path = "E:\\pdd";
        File f = new File(path);
        //Determine whether it's a file or a directory
        System.out.println(f.isFile());
        System.out.println(f.isDirectory());
    }
}
image.png

There are many other ways to use File classes. It is recommended to query documents when using File classes.

Byte stream

  • Byte input stream: InputStream (read)
  • Byte Output Stream


    image.png
image.png
FileInputStream
  • Read raw byte streams such as image data, such as images, bytes in files.


    image.png
image.png
public class FileDemo {
    public static void main(String[] args) {
        String path = "";

        // FileInputStream,
        try {
            FileInputStream fs = new FileInputStream(path);
            try {
                
                int n = fs.read();
                System.out.print((char)n);
                fs.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Attention should be paid to the inheritance of exceptions in the code. Outer exceptions cannot be subclasses of inner exceptions.

FileOutputStream

Write data to a file.


image.png
image.png
Buffer flow
  • BufferedInputStream
  • BufferedOutputStream
    Buffer streams, when the buffer is full, commit, reducing frequent write operations.
public class FileDemo {
    public static void main(String[] args) {
        String path = "";
        try {
            FileOutputStream fos = new FileOutputStream(path);
            BufferedOutputStream bos = new BufferedOutputStream(fos);

            FileInputStream fis = new FileInputStream(path);
            BufferedInputStream bis = new BufferedInputStream(fis);

            bos.write(50);
            bos.write('a');
            //Submit data to file
            bos.flush();
            bos.close();

            System.out.println(bis.read());
            bis.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Character stream

  • Character input stream: Reader
  • Character output stream: Writer
Byte character conversion stream
  • InputStreamReader input
  • Output Stream Writer output

Keep the coding consistent when reading and writing data.

Object serialization and deserialization

Object serialization steps:

  1. Create a Class Inheritance Serializable Interface
  2. create object
  3. Processing objects (write files, send http, store, etc.)
  4. Read alignment
  • ObjectInputStream object input stream
  • ObjectOutputStream object output stream

Example:
Create a new Goods.java file, define a Goods class and inherit the Serializable interface

public class Goods implements Serializable{
    private String goodsId;
    private String goodsName;
    private double price;

    @Override
    public String toString() {
        return "Goods{" +
                "goodsId='" + goodsId + '\'' +
                ", goodsName='" + goodsName + '\'' +
                ", price=" + price +
                '}';
    }

    public Goods(String goodsId, String goodsName, double price){
        this.goodsId = goodsId;
        this.goodsName = goodsName;
        this.price = price;
    }

    public String getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(String goodsId) {
        this.goodsId = goodsId;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

Build another test entry file named GoodsTest.java

public class GoodsTest {
    public static void main(String[] args) {
        String path = "1.txt";
        Goods gd = new Goods("001", "allen", 6.0);

        try {
            FileOutputStream fos = new FileOutputStream(path);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(gd);
            oos.writeBoolean(true);
            oos.flush();
            oos.close();
            System.out.println("-----------------------------------");
            FileInputStream fis = new FileInputStream(path);
            ObjectInputStream ois = new ObjectInputStream(fis);
            try {
                Goods gt = (Goods)ois.readObject();
                boolean b = ois.readBoolean();
                System.out.println(gt);
                System.out.println(b);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            ois.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

Store and read the file named 1.txt.

Note: Develop the habit of looking at official documents, because after all, some methods are rarely used. When you use them, you can refer to them. There are no details about the methods of classes in the article. Please refer to the official manual, which is very clear.

If the article is helpful to you, remember to praise it. Pay attention to the author's first update.
Good luck ~ ~

Posted by Xajel on Sat, 11 May 2019 08:01:30 -0700