Session 10 |
Input and output of primitive data types
Imagine an e-commerce application which saves the data about each item (item's name, quantity and price delimited by a space). Later if we want to read the file, we have to use different methods such as String.split
, Integer.parseInt
etc. The task is simple, but the data processing is complicated.
Another solution would be to set the format of that data at first using the classes DataOutputStream
and DataInputStream
.
Study how it is possible to write the objects into the 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); } }
Session 10 |