Results 1 to 2 of 2
  1. #1
    ben2470 is offline Member
    Join Date
    Aug 2014
    Posts
    6
    Rep Power
    0

    Default Replacing a binary data

    Below is a method that i want to try to renew the expiry date.
    Well I'm trying to read the file and rewrite the data into the txt file, is that possible?

    Java Code:
     public static void renew()throws IOException{
        	Scanner s=new Scanner(System.in);
        	Scanner s2=new Scanner(System.in);
        	Calendar calendar = new GregorianCalendar();
        	int year = (calendar.get(Calendar.YEAR));
        	int month = (calendar.get(Calendar.MONTH));
        	int date = (calendar.get(Calendar.DATE));
        	String expiredDate = ( (year+1) + "/" + month + "/" + date );
        	int choice;
        	String vipName,vipGender,vipAddress,vipPhone,vipExp,corpName,corpGender,corpAddress,corpPhone,corpExp;
        	int vipId,corpId;
        	double vipFee,corpFee;
        	System.out.println("RENEW MEMBERSHIP");
        	System.out.println("================");
        	System.out.println("Please choose the type of membership");
        	System.out.println("1. VIP");
        	System.out.println("2. Corporate");
        	System.out.println("3. Back ");
        	System.out.print("Your choice :");
        	choice=s.nextInt();
        	System.out.print("Please enter your member ID");
        	int memId=s2.nextInt();
        	DataInputStream din= new DataInputStream(new FileInputStream("vip.txt"));
        	DataInputStream din2= new DataInputStream(new FileInputStream("corporate.txt"));
        	DataOutputStream dout= new DataOutputStream(new FileOutputStream("vip.txt"));
        	DataOutputStream dout2= new DataOutputStream(new FileOutputStream("corporate.txt"));
        		
        		if(choice==1){
        		
        		while(din.available()!=0){
        			vipName=din.readUTF();
        			vipGender=din.readUTF();
        			 vipAddress=din.readUTF();
        			 vipPhone=din.readUTF();
        			 vipId=din.readInt();
        			 vipFee=din.readDouble();
        			 vipExp=din.readUTF();
        		}
        		if(memId==vipId){
        			System.out.println("Renew success.");
        			System.out.println("Your expiry date will be "+expiredDate);
        			dout.writeUTF(vipName);
        			dout.writeUTF(vipGender);
        			dout.writeUTF(vipAddress);
        			dout.writeUTF(vipPhone);
        			dout.writeInt(vipId);
        			dout.writeDouble(vipFee);
        			dout.writeUTF(expiredDate);
        		}
        		else {
        			System.out.println("Wrong member ID!");
        			renew();
        		}
        			
        }
        else if(choice==2){
        	while(din2.available()!=0){
        			 corpName=din.readUTF();
        			 corpGender=din.readUTF();
        			 corpAddress=din.readUTF();
        			 corpPhone=din.readUTF();
        			 corpId=din.readInt();
        			 corpFee=din.readDouble();
        			 corpExp=din.readUTF();
        		}
        		if(memId==corpId){
        			System.out.println("Renew success.");
        			System.out.println("Your expiry date will be "+expiredDate);
        			dout.writeUTF(corpName);
        			dout.writeUTF(corpGender);
        			dout.writeUTF(corpAddress);
        			dout.writeUTF(corpPhone);
        			dout.writeInt(corpId);
        			dout.writeDouble(corpFee);
        			dout.writeUTF(expiredDate);
        		}
        		else {
        			System.out.println("Wrong member ID!");
        			renew();
        		}
        }
        else if(choice==3)
        	mainmenu();
        	else{
        		System.out.println("Please enter either 1, 2, or 3!");
        		renew();
        		}
        din.close();
        din2.close();
        }
    Last edited by JosAH; 08-04-2014 at 09:31 AM. Reason: changed to [code] ... [/code] tags

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Replacing a binary data

    What happens with the above code?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. parsing binary data to ASCII with javacc
    By nab2329 in forum New To Java
    Replies: 6
    Last Post: 05-24-2014, 04:08 AM
  2. Binary Tree Data Structure
    By amportugal in forum New To Java
    Replies: 11
    Last Post: 06-03-2012, 07:31 PM
  3. Adding data to an existing binary file
    By NatProg in forum Advanced Java
    Replies: 5
    Last Post: 03-15-2012, 01:51 AM
  4. Reading BInary Data
    By prich in forum Networking
    Replies: 3
    Last Post: 09-05-2009, 03:28 AM
  5. Reading Binary File and Changing data
    By janakiram.attuluri in forum Advanced Java
    Replies: 1
    Last Post: 12-21-2007, 11:10 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •