Jump to content


Writing objects to files


  • Please log in to reply
1 reply to this topic

#1 _*MasterMind_*

_*MasterMind_*
  • Guests

Posted 01 December 2007 - 08:53 AM

Java problem!!!

I want to write objects to a file... how can I do that????

/**
* Write a description of class TDMS here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.io.*;
public class TDMS
{
long rec_id;
String person_name;
String d_o_b;
String res_adress;
int res_pincode;
long res_phoneno;
int res_faxno;
String occupation;
String companyName;
String officeAddress;
int officePinCode;
long officePhoneno;
long officeFax;
String emailId;
String internetAdress;
public TDMS()
{
rec_id=0;
person_name=" ";
d_o_b=" ";
res_adress=" ";
res_pincode=0;
res_phoneno=0;
res_faxno=0;
occupation=" ";
companyName=" ";
officeAddress=" ";
officePinCode=0;
officePhoneno=0;
officeFax=0;
emailId=" ";
internetAdress=" ";
}
public void get_data()throws IOException
{
rec_id++;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter your name: ");
person_name=br.readLine();
System.out.print("Enter your date of birth(dd/mm/yy): ");
d_o_b=br.readLine();
System.out.print("Enter your residential address: ");
res_adress=br.readLine();
System.out.print("Enter your residential pincode: ");
res_pincode=Integer.parseInt(br.readLine());
System.out.print("Enter your residential phone number: ");
res_phoneno=Integer.parseInt(br.readLine());
System.out.print("Enter your residential fax number: ");
res_faxno=Integer.parseInt(br.readLine());
System.out.print("Enter your occupation: ");
occupation=br.readLine();
System.out.print("Enter your company name: ");
companyName=br.readLine();
System.out.print("Enter your office adress: ");
officeAddress=br.readLine();
System.out.print("Enter your office pin code: ");
officePinCode=Integer.parseInt(br.readLine());
System.out.print("Enter your office phone number: ");
officePhoneno=Integer.parseInt(br.readLine());
System.out.print("Enter your office fax: ");
officeFax=Integer.parseInt(br.readLine());
System.out.print("Enter your e-mail ID: ");
emailId=br.readLine();
System.out.print("Enter your web address: ");
internetAdress=br.readLine();
}
public void show_data()
{
System.out.println("Record ID: "+rec_id);
System.out.println("Name :"+person_name);
System.out.println("Date of Birth: "+d_o_:D;
System.out.println("Residential address: "+res_adress);
System.out.println("Residential pin code: "+res_pincode);
System.out.println("Residential phone number: "+res_phoneno);
System.out.println("Residential fax number: "+res_faxno);
System.out.println("Occupation: "+occupation);
System.out.println("Company name: "+companyName);
System.out.println("Office adress: "+officeAddress);
System.out.println("Office pin code: "+officePinCode);
System.out.println("Office phone number: "+officePhoneno);
System.out.println("Office fax: "+officeFax);
System.out.println("E-Mail ID: "+emailId);
System.out.println("Web adress: "+internetAdress);
}
}

What addition should I do to get an object say o1 of the getData function???

#2 dotbart

dotbart

    Young Padawan

  • Members
  • Pip
  • 141 posts
  • Gender:Male
  • Location:Diepenbeek
  • Interests:Webdesign, Webdeveloppement, DJ, ...

Posted 09 December 2007 - 06:41 AM

You should use the Serializable interface.

e.g.

import java.io.*;
public class ReadYourObject implements Serializable{
/**
* Your code goed here
*/


/**
* Save your object to a file (the object 'ObjectToWrite)
*/

	public void save() throws IOException{
		File file = new File("c:\\test.txt");
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
		out.writeObject(ObjectToWrite);
		out.close();
	}

/**
* Read an object from a file (into ObjectToReadInto)
*/
	public void readFrom(String filename) throws IOException{
		File file = new File(filename);
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(bestand));
		try{
			ObjectToReadInto = (PassagiersLijst)in.readObject();
		}catch(ClassNotFoundException e){
			System.err.println("Class not found");
		}
		in.close();
	}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users