import java.io.*;
class BinaryFilesUpdate
{   void createMaster()throws IOException
    {   FileOutputStream fos=new FileOutputStream("M.DAT",false);
        DataOutputStream dos=new DataOutputStream(fos);
        String name="Charles"; int amount=1000;
        dos.writeUTF(name); dos.writeInt(amount);
        name="Bill"; amount=500;
        dos.writeUTF(name); dos.writeInt(amount);
        dos.close(); fos.close();
    }//create    
    void createTrans()throws IOException
    {   FileOutputStream fos=new FileOutputStream("T.DAT",false);
        DataOutputStream dos=new DataOutputStream(fos);
        String name="Charles"; int amount=500; char type='w';
        dos.writeUTF(name); dos.writeInt(amount); dos.writeChar(type);
        name="Bill"; amount=250; type='d';
        dos.writeUTF(name); dos.writeInt(amount);dos.writeChar(type);
        name="Charles"; amount=700; type='d';
        dos.writeUTF(name); dos.writeInt(amount);dos.writeChar(type);
        dos.close(); fos.close();
    }//create    
    void update()throws IOException
    {   FileInputStream fis=new FileInputStream("M.DAT");
        DataInputStream dis=new DataInputStream(fis);
        FileOutputStream fos=new FileOutputStream("TEMP.DAT",false);
        DataOutputStream dos=new DataOutputStream(fos);
        String name; int amount;
        boolean endOfFile=false;
        while(!endOfFile)
        {   try{
                name=dis.readUTF();
                amount=dis.readInt();
                FileInputStream fis1=new FileInputStream("T.DAT");
                DataInputStream dis1=new DataInputStream(fis1);
                boolean endOfFile1=false;
                String n; int a; char c;
                while(!endOfFile1)
                {   try{    n=dis1.readUTF();
                             a=dis1.readInt();
                             c=dis1.readChar();
                             if(n.equals(name))
                             {  if(c=='w') amount-=a;
                                else amount+=a;
                             }
                         }//try of Transaction file
                         catch(EOFException e)
                         {   endOfFile1=true;
                         }
                }//inner while                            
                dis1.close(); fis1.close();
                dos.writeUTF(name);
                dos.writeInt(amount);                
            }//try of Master
            catch(EOFException e)
            {   endOfFile=true;
            }
        }//while
        dis.close(); fis.close();        
        //copy file TEMP.DAT  to M.DAT & display M.DAT
    }//update
    public static void main(String args[])throws IOException
    {   BinaryFilesUpdate obj=new BinaryFilesUpdate();
    }//main
}//class 	
