CS2514

Introduction to Java

Dr Derek Bridge

School of Computer Science & Information Technology

University College Cork

Problems at run-time

Exception handling

Reading & writing bytes

In a word, what does this program do?

import java.io.*;

class Mystery {

    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream(new File("file1.txt"));
            FileOutputStream fos = new FileOutputStream(new File("file2.txt"));
            while (true) {
                int nextByte = fis.read();
                if (nextByte == -1) {
                    break;
                }
                fos.write(nextByte);
            }
            fis.close();
            fos.close();
        } catch (IOException e) {
            System.out.println("Sorry something has gone wrong: " + e.getMessage());
        }
    }
}

Chaining streams together

Serialization: reading/writing objects

LibraryBook

import java.io.Serializable;

class LibraryBook implements Serializable {
            
    private String author;
    private String title;
    private int numCopiesPossessed;
    private int numCopiesOnLoan;
    
    public LibraryBook(String author, String title) {
        this.author = author;
        this.title = title;
    }
    
    public LibraryBook(String author, String title, int numCopies) {
        this(author, title);
        this.numCopiesPossessed = numCopies;
    }
    
    public int getNumCopiesPossessed() {
        return numCopiesPossessed;
    }
    
    public int getNumCopiesOnLoan() {
        return numCopiesOnLoan;
    }
    
    public int getNumCopiesOnShelves() {
        return numCopiesPossessed - numCopiesOnLoan;
    }
    
    public void acquire() {
        numCopiesPossessed = numCopiesPossessed + 1;
    }
    
    public void acquire(int numNewCopies) {
        numCopiesPossessed = numCopiesPossessed + numNewCopies;
    }
    
    public void takeOut() {
        numCopiesOnLoan = numCopiesOnLoan + 1;
    }
    
    public void bringBack() {
        numCopiesOnLoan = numCopiesOnLoan - 1;
    }
    
    public String toString() {
        return author + ": " + title + ", " + numCopiesPossessed + " copies";
    }
    
}

Writing and reading an object

Library

import java.io.Serializable;

class Library implements Serializable {

    private LibraryBook[] books;
    
    public Library() {
        books = new LibraryBook[0];
    }
    
    public void addBook(LibraryBook book) {
        LibraryBook[] temp = new LibraryBook[books.length + 1];
        for (int i = 0; i < books.length; i = i + 1) {
            temp[i] = books[i];
        }
        temp[books.length] = book;
        books = temp;
    }
    
    public String toString() {
        String str = "";
        for (int i = 0; i < books.length; i = i + 1) {
            str = str + books[i] + "\n";
        }
        return str;
    }

}

So what happens here?

import java.util.Scanner;
import java.io.*;

class LibraryTester {

    public static void main(String[] args) {
        
        try {
            Library lib;
            File file = new File("library.ser");
            if (file.exists()) {
                ObjectInputStream ois = 
                    new ObjectInputStream(
                        new BufferedInputStream(new FileInputStream(file)));
                lib = (Library) ois.readObject();
                ois.close();
            } else {
                lib = new Library();
            }
            
            System.out.println(lib);
            
            Scanner sc = new Scanner(System.in);
            System.out.print("Author: ");
            String author = sc.nextLine();
            System.out.print("Title: ");
            String title = sc.nextLine();
            System.out.print("Copies acquired: ");
            int numNewCopies = sc.nextInt();
            LibraryBook b = new LibraryBook(author, title, numNewCopies);
            lib.addBook(b);
        
            ObjectOutputStream oos = 
                new ObjectOutputStream(
                    new BufferedOutputStream(new FileOutputStream(file)));
            oos.writeObject(lib);
            oos.close();
        } catch (IOException e) {
            System.out.println("Sorry - unable to read or write to file");
        } catch (ClassNotFoundException e) {
            System.out.println("Sorry - file does not contain a library");
        }
    }
}