|  | Chapter 9 |  | 
Data streams
Data streams are particularly suitable for reading and writing primitive data to and from streams. The primitive data type values can be a String or int, long, float, double, byte, short, boolean, and char. The direct implementation classes for Data I/O stream are DataInputStream and DataOutputStream. Study how it is possible to write data into a file:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 class Item {
    private String name;
    private double quantity;
    private double price;
    public Item(String name, double quantity, double price) {
        this.name = name;
        this.quantity = quantity;
        this.price = price;
    }
    //save data about items
    public void mySave(DataOutputStream dos) throws Exception {
        //pay attention to the write methods and data types
        dos.writeUTF(name);
        dos.writeDouble(quantity);
        dos.writeDouble(price);
    }
    //read data about items
    public static Item myRead(DataInputStream dis) throws Exception {
        //pay attention to the read methods and data types
        String name = dis.readUTF();
        double quantity = dis.readDouble();
        double price = dis.readDouble();
        return new Item(name, quantity, price);
    }
     @Override
     public String toString() {
         return name + ": " + " quantity=" + quantity + " for " + price + '€';
     }
 }
 class Basket {
     private String client;
     private List<Item> items;
     public Basket(String client, List<Item> items) {
         this.client = client;
         this.items = items;
     }
     public void mySave(DataOutputStream dos) throws Exception {
         //pay attention to the write methods and data types
         dos.writeUTF(client);         //save data about client
         dos.writeInt(items.size());  //add the number of items in the basket to ease reading
         //save data about items in the client's basket
         for (Item i : items) {
             i.mySave(dos);
         }
     }
     public static Basket myRead(DataInputStream dis) throws Exception {
         //read data about client
         String client = dis.readUTF();
         //read data about items in the client's basket
         List<Item> items = new ArrayList<>();
         int rItems = dis.readInt(); // instantiate variable for the loop
         for (int i = 0; i < rItems; i++) {
             items.add(Item.myRead(dis));
         }
         return new Basket(client, items);
     }
     @Override
     public String toString() {
         return client + ": " + items;
     }
 }
class Test12 {
    public static void main(String[] args) throws Exception {
        Item chocolate = new Item("chocolate", 2.0, 1.29);
        Item orange = new Item("orange", 2.5, 0.89);
        Basket myBasket = new Basket("Mary", Arrays.asList(chocolate, orange));
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.bin"));
        myBasket.mySave(dos);
        dos.close();
        DataInputStream dis = new DataInputStream(new FileInputStream("data.bin"));
        Basket dataFromFile = myBasket.myRead(dis); // the same as at the beginning
        dis.close();
        System.out.println(dataFromFile);
    }
}
|  | Chapter 9 |  |