![]() | 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
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 | ![]() |