Results 1 to 4 of 4
  1. #1
    idi
    idi is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default Storing data to file :s

    Am kind of new to java so please dont attack me!

    I have a MainForm class which is the GUI for the user. The user enters data such as name age DOB etc. I have a Student class which extends the person class and has all the relevant get and sets. All the data from the MainForm is set in the following way:

    Java Code:
    Student method = new Student();
    
    String stemail = txtemail.getText();       //sets email
    method.setAddress(stemail);
    
    String stname = txtname.getText();       //sets name
    method.setName(stname);
    I need to be able to save around 200 records and and view the next and previous in the textboxes. and then i need to store the all the records to a store class and then save the store class in a file so it can be opened up again :confused: i have no idea how to do this. can anyone shed some light on this or give me a link to read up on.

    Here is the store class

    Java Code:
    import java.io.*;
    
    public class Store implements Serializable
    {
    	private Person list[];
    	private int count;
    	private int maxSize;
    	private int current = 0;
    	private Store outputstore;
    
    	public Store(int max)
    	{
    		list = new Person[max];
    		maxSize = max;
    		count = 0;
    
    	}
    
    	public void add (Person p)
    	{
    		if (!isFull() && !isIn(p))
    		{
    			list[count] =p;
    			count++;
    		}
    		else
    		{
    			System.out.println("Store is full.");
    			System.out.println("");
    		}
    	}
    
    	public boolean isFull()
    	{
    		return count == maxSize;
    	}
    
    	public boolean isEmpty()
    	{
    		return count==0;
    	}
    
    	public boolean isIn(Person p)
    	{
    		for (int index = 0; index < count; index++)
    		{
    			if(list[index].equals(p))
    			{
    				return true;
    			}
    		}
    		return false;
    	}
    
    	public int getCount()
    	{
    		return count;
    	}
    
    	public Person currentRecord()
    	{
    		return list[current];
    	}
    
    	public void fileout2(String fileName)
    	{
    		try
    		{
    			ObjectOutputStream oos = null;
    			oos = new ObjectOutputStream(new FileOutputStream(fileName));
    			oos.writeObject(this);
    		}
    		catch(IOException e)
    		{
    			System.out.println("Error outputting file: " + e);
    		}
    	}
    
    	public Store fileIn2(String fileName)
    	{
    		Object o = null;
    		try
    		{
    			ObjectInputStream ois = null;
    			ois = new ObjectInputStream(new FileInputStream(fileName));
    			o = ois.readObject();
    		}
    
    		catch (IOException io)
    		{
    			System.out.println("Error file input: " + io);
    		}
    
    		catch (ClassNotFoundException cnf)
    		{
    			System.out.println("Error file input: " + cnf);
    		}
    
    		return(Store)o;
    	}
    
    	public void incrementCurrentPointer()
    	{
    		this.current++;
    		if (current>=count)
    		{
    			current=0;
    		}
    
    	}
    
    	public void decrementCurrentPointer()
    	{
    		this.current--;
    		if(current<0)
    		{
    			current=count-1;
    		}
    	}
    
    	public void displayAll()
    	{
    		for (int index=0; index < count; index++)
    			System.out.println(list[index]);
    
    	}
    }

  2. #2
    wtd_nielsen is offline Member
    Join Date
    Dec 2009
    Posts
    24
    Rep Power
    0

    Default

    you could choose to store the Store object or just the Person list[]. Remember that your Person class also needs to be serializable (in both cases).
    public void fileout2(Store storeObj)
    or
    public void fileout2(Person[] list)

  3. #3
    idi
    idi is offline Member
    Join Date
    Feb 2010
    Posts
    5
    Rep Power
    0

    Default

    so if i wanted to add more than 1 record. how do i do that? so i can view the full list of people that has been saved?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by idi View Post
    Am kind of new to java so please dont attack me!
    I have a MainForm class which is the GUI for the user. The user enters data such as name age DOB etc. I have a Student class which extends the person class and has all the relevant get and sets. All the data from the MainForm is set in the following way:

    //...

    I need to be able to save around 200 records and and view the next and previous in the textboxes. and then i need to store the all the records to a store class and then save the store class in a file so it can be opened up again :confused: i have no idea how to do this. can anyone shed some light on this or give me a link to read up on.

    Here is the store class
    //...
    So, what I want to know is this: how is your current Store class not working? On cursory glance it appears to store itself OK (complete with its array of Persons) other than you don't appear to be flushing or closing the ObjectOutputStream.

Similar Threads

  1. Storing data?
    By Syntax in forum New To Java
    Replies: 4
    Last Post: 01-23-2010, 02:17 AM
  2. Storing the data in a file
    By vasavi.singh in forum New To Java
    Replies: 4
    Last Post: 02-20-2009, 05:01 PM
  3. Storing data from text file in ArrayList
    By tjhodge in forum New To Java
    Replies: 1
    Last Post: 02-12-2009, 02:22 PM
  4. Storing data permanently
    By shaungoater in forum New To Java
    Replies: 2
    Last Post: 03-10-2008, 05:18 PM
  5. Storing Data
    By Khorod in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 06:48 AM

Posting Permissions

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