Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Data from file into ArrayList

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Data from file into ArrayList

    Hello,

    I have written a program which includes this code

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.util.ArrayList;
    import java.util.HashSet;
     
    /**
     * The purpose of this class is to be an
     * ArrayList manager. It helps manage the
     * ArrayLists and the HashSet and have
     * access to them and their data.
     */
    public class ArrayListManager {
     
     
        private ArrayList<Admin> admin;
        private ArrayList<Manager> manager;
        private ArrayList<Employee> employee;
        private ArrayList<Keyboard> keyboard;
        private ArrayList<Mouse> mouse;
        private ArrayList<Printer> printer;
        private ArrayList<StorageUnit> storageUnit;
        private ArrayList<Order> order;
        private HashSet<String> mySet;
     
    /**
         * This is the constructor of the class ArrayListManager. 
         * It creates eight different arraylists and one HashSet.
         */
        public ArrayListManager()
        {
            admin = new ArrayList<Admin>();
            manager = new ArrayList<Manager>();
            employee = new ArrayList<Employee>();
            keyboard = new ArrayList<Keyboard>();
            mouse = new ArrayList<Mouse>();
            printer = new ArrayList<Printer>();
            storageUnit = new ArrayList<StorageUnit>();
            order = new ArrayList<Order>();
            mySet = new HashSet<String>();
        }
     
        /**
         * This function returns an admin ArrayList.
         * @return The list with the administrators.
         */
        public ArrayList getArrayAdmin()
        {
            return admin;
        }
     
        /**
         * This function returns a manager ArrayList.
         * @return The list with the managers.
         */
        public ArrayList getArrayManager()
        {
            return manager;
        }
     
        /**
         * This function returns an employee ArrayList.
         * @return The list with the employees.
         */
        public ArrayList getArrayEmployee()
        {
            return employee;
        }
     
        /**
         * This function returns the ArrayList keyboard.
         * @return The list with the keyboards.
         */
        public ArrayList getArrayKeyboard()
        {
            return keyboard;
        }
     
        /**
         * This function returns the ArrayList mouse.
         * @return The list with the mice.
         */
        public ArrayList getArrayMouse()
        {
            return mouse;
        }
     
        /**
         * This function returns the ArrayList printer.
         * @return The list with the printers.
         */
        public ArrayList getArrayPrinter()
        {
            return printer;
        }
     
        /**
         * This function returns the ArrayList storageUnit.
         * @return The list with the storage units.
         */
        public ArrayList getArrayStorageUnit()
        {
            return storageUnit;
        }
     
        /**
         * This function returns the ArrayList order.
         * @return The list with the orders.
         */
        public ArrayList getArrayOrder()
        {
            return order;
        }
     
     /**
         * This functions gets the data from the files Admin.dat, Manager.dat, Employee.dat,
         * Keyboard.dat, Mouse.dat, Printer.dat, StorageUnit.dat, Order.dat and MySet.dat.
         * In this way, any changes that had been saved during a previous execution of the
         * programme are loaded again.
         */
        public void getData() {
    		FileInputStream fis = null;
    		ObjectInputStream in = null;
    		try {
    			fis = new FileInputStream("Admin.dat");
    			in = new ObjectInputStream(fis);
    			admin = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}
     
    		try {
    			fis = new FileInputStream("Manager.dat");
    			in = new ObjectInputStream(fis);
    			manager = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}
     
                    try {
    			fis = new FileInputStream("Employee.dat");
    			in = new ObjectInputStream(fis);
    			employee = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}
     
                    try {
    			fis = new FileInputStream("Keyboard.dat");
    			in = new ObjectInputStream(fis);
    			keyboard = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}
     
                    try {
    			fis = new FileInputStream("Mouse.dat");
    			in = new ObjectInputStream(fis);
    			mouse = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}
     
                    try {
    			fis = new FileInputStream("Printer.dat");
    			in = new ObjectInputStream(fis);
    			printer = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}
     
                    try {
    			fis = new FileInputStream("StorageUnit.dat");
    			in = new ObjectInputStream(fis);
    			storageUnit = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}
     
                    try {
    			fis = new FileInputStream("Order.dat");
    			in = new ObjectInputStream(fis);
    			order = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}
     
    	}
    }

    Instead of having this code reiteration at getData function, I would like to write a function getData(ArrayList array, String filename).

    public void getData(ArrayList array, String filename) {
    		FileInputStream fis = null;
    		ObjectInputStream in = null;
    		try {
    			fis = new FileInputStream(filename);
    			in = new ObjectInputStream(fis);
    			array = (ArrayList) in.readObject();
    			in.close();
    		} catch (IOException ex) {
    			ex.printStackTrace();
    		} catch (ClassNotFoundException ex) {
    			ex.printStackTrace();
    		}

    The problem with this function is when I try to load the data form the files to the arraylists.

    At my main class I call this function by writting

    ArrayListManager list = new ArrayListManager();
    list.getData(list.getArrayAdmin(), "Admin.dat");
    list.getData(list.getArrayManager(), "Manager.dat");
    ....
    list.getData(list.getArrayStorageUnit(), "StorageUnit.dat");

    The problem is that when I print the size of the arraylists using the function list.getArrayAdmin().size(), it is 0 which shows that the data is not loaded.

    I would appreciate your help.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,357
    My Mood
    Hungover
    Thanks
    144
    Thanked 626 Times in 537 Posts

    Default Re: Data from file into ArrayList

    Crossposted: Load data from file to ArrayList - Java Forums
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Using File Data
    By computercoder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 18th, 2010, 07:51 PM
  2. Cannot update data in .txt/.csv file
    By Azriq007 in forum JDBC & Databases
    Replies: 2
    Last Post: October 16th, 2010, 09:16 PM
  3. [SOLVED] Compressed data from XML file
    By eclipse in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: July 15th, 2010, 02:39 PM
  4. [SOLVED] Reading from a text file and storing in arrayList
    By nynamyna in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 26th, 2010, 09:55 PM
  5. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM