Writing into RandomAccessFile then re-printing
<p>How would I rewrite the new stock level back into the <code>books.dat</code> file and reprint the inventory with the new values? The problem is in <code>UpdateStock()</code> method.</p>

<pre><code>import java.util.*;
import java.io.*;
/*class Book{  // this was my first attempt using a book class and arraylist.
    public final String isbn;
    public final String title;
    public final String author;
    public final int level;
    public Book(String isbn, String title, String author, int level) {
        this.isbn = isbn; this.title = title; this.author = author; this.level = level;
    }
    String getisbn(){
        return isbn;
    }
    String getTitle(){
        return title;
    }
    String getAuthor(){
        return author;
    }
    int getLevel(){
        return level;
    }
} 
class Transaction {
    private String isbn;
    private int level;

    Transaction(String isbn, int level) {
        this.isbn = isbn;
        this.level = level;
    }
}*/
class Inventory { 
    void Stock(){ 
        try{// in this method i simply format the information when printing it
            RandomAccessFile in = new RandomAccessFile("books.dat","r"); //created the reader called "in" to read the Random Access File "books.dat".
            String ISBN,authors,titles;
            int levels;
            while(in.getFilePointer()&lt;in.length()) { // while loop to continue reader in the file until it reaches the end of the file
                //bkList1.add(new Book("isbn","title","author",level));
                authors = in.readUTF(); titles = in.readUTF(); ISBN = in.readUTF(); // intialise the variables
                levels = in.readInt();
                System.out.printf("%10s %-20s %-35s %10s \n", ISBN, authors, titles, levels); // format the information as described in the brief 
            }
            System.out.println(); // creates a space between the stock and inventory.
            in.close();//this close the"in" file
        }
        catch(IOException e) { // this try catch block catchs any errors
            e.printStackTrace();
        }
    }
    void Inventory(){ 
        try { // i take in books.dat to receive the information again to compare it to the isbn's of the transactions.txt
            RandomAccessFile in = new RandomAccessFile("books.dat","r");//created the reader called "in" to read the Random Access File "books.dat".
            Scanner out = new Scanner(new File("transactions.txt"));//created the scanner called "out" to read "transactions.txt".
            String ISBN,ISBN2, authors, titles; // intialise the variables
            int levels,level2;
            while(out.hasNext()) { // while loop to continue reading while the txt file has another line i.e. till the end
                ISBN2 = out.next();
                level2 = out.nextInt();
                while(in.getFilePointer()&lt;in.length()){ // while loop to continue reader in the file until it reaches the end of the file to get the data to compare with transaction.txt 
                    authors = in.readUTF(); titles = in.readUTF(); ISBN = in.readUTF(); // intialise the variables once again
                    levels = in.readInt();
                    if (ISBN.equals(ISBN2)){ //if the books.dat file has isbns the same as transaction.txt print out the title of the book.
                        System.out.printf("%-35s %5s \n", titles, level2); // formats the information as described in the assignment brief  ( this is the 80% part of the assignment
                        in.seek(0);// Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs
                        break; //this breaks the loop
                    }   
                }
            }
            in.close();//this close the"in" file
            out.close();//this close the "out" file
        }
        catch(IOException f){ // this try catch block catchs any errors
            f.printStackTrace();
        }
    }
    void UpdateStock(){ // this method is an attempt at the 90% part 
        try {
            RandomAccessFile in = new RandomAccessFile("books.dat","rw");
            Scanner out = new Scanner(new File("transactions.txt"));//created the scanner called "out" to read "transactions.txt".
            System.out.println();
            String ISBN,ISBN2,authors,titles; // intialise the variables once again
            int levels,level2,stock; 
            while(out.hasNext()) { // while loop to continue reading while the txt file has another line 
                ISBN2 = out.next();
                level2 = out.nextInt();         
                while(in.getFilePointer()&lt;in.length()){
                    authors = in.readUTF(); titles = in.readUTF(); ISBN = in.readUTF();
                    levels = in.readInt();
                    stock = levels + level2;
                    if (ISBN.equals(ISBN2)){
                        stock = levels;
                        in.writeUTF(authors,titles,ISBN);
                                            in.writeInt(levels);
                        //System.out.printf("%-40s %5s \n", titles, stock);
                        in.seek(0); 
                        break;// breaks the loop.
                    }   
                }
            }
            in.close();//this close the"in" file
            out.close();//this close the "out" file
        }
        catch(IOException g){
            g.printStackTrace();
        }
    }
}
class StockManage{
    public static void main(String [] args){
        Inventory books = new Inventory();
        books.Stock();// calling the stock method
        books.Inventory();// calling the inventory method
        books.UpdateStock();// calling the UpdateStock method
        books.Stock();
    }
}
</code></pre>
