Institute of Computer Science
  1. Courses
  2. 2018/19 spring
  3. Object-Oriented Programming (Narva College) (LTAT.NR.003)
ET
Log in

Object-Oriented Programming (Narva College) 2018/19 spring

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links
Session 10

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);

    }
}
Session 10
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment