Results 1 to 8 of 8
  1. #1
    binweifong is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default saving defined arraylist into FILE ! DUE TOMORROW ! HELP !!!

    public static void UpdateFile(ArrayList p) throws IOException
    {
    FileWriter fWriterStream = new FileWriter ("StudList.txt");
    BufferedWriter out = new BufferedWriter (fWriterStream);
    Object [] ar = p.toArray(ar);



    for(int i = 0; i <p.size(); i++)
    {
    out.writeObject(ar);
    }
    out.close();
    }


    // the code above is not working ! it says 'out' is not found !i have an array list passed to a method (shown above), because ive ammended its content, so at the end of the code, i have to save it back to the textfile named "StudList.txt", i check online to diffferentiate bufferedreader, output, bla bla, but i cannot adapt any of the concepts since i was facing code from this morning, help !!!

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by binweifong View Post
    public static void UpdateFile(ArrayList p) throws IOException
    {
    FileWriter fWriterStream = new FileWriter ("StudList.txt");
    BufferedWriter out = new BufferedWriter (fWriterStream);
    Object [] ar = p.toArray(ar);



    for(int i = 0; i <p.size(); i++)
    {
    out.writeObject(ar);
    }
    out.close();
    }


    // the code above is not working ! it says 'out' is not found !i have an array list passed to a method (shown above), because ive ammended its content, so at the end of the code, i have to save it back to the textfile named "StudList.txt", i check online to diffferentiate bufferedreader, output, bla bla, but i cannot adapt any of the concepts since i was facing code from this morning, help !!!

    usually, using the method writeObject() you must use the class ObjectOutputStream. second, i got errors with Object [] ar = p.toArray(ar); saying "The local variable ar may not have been initialized". please fix this error.

  3. #3
    binweifong is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    ar is a new variable i defined to store the arrayList, i dunno what is wrong, im getting that error too ):

  4. #4
    binweifong is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    okay i know whats the problem, but im now having a bigger one ): duh
    when i exit the program, it suppose to save my file into the textfile, but it gives me error saying that notserializableException. -.- ive never learn that before

  5. #5
    xmdx's Avatar
    xmdx is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    u should implement serialization to objects, which you are trying to save

  6. #6
    banhbaochay is offline Member
    Join Date
    Apr 2010
    Posts
    32
    Rep Power
    0

    Default

    Patient, you must write to txt file an object??
    If it's needed, you can read below link, it's good for you:
    Writing objects to file with ObjectOutputStream - A Java Code Example

  7. #7
    binweifong is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    ive read that before, but it differs ): cause my arraylist is already defined, im not trying to set what to save into the textfile AT LAST, im trying to save the already-modified-arraylist into the textfile ): pleaseeeeeeeeeee

  8. #8
    banhbaochay is offline Member
    Join Date
    Apr 2010
    Posts
    32
    Rep Power
    0

    Default

    @binweifong:
    I think it's not differrence between them: itself-defined or already-defined.
    You can use below method for writing object to file:
    Java Code:
    void writeObjectToFile(ArrayList p, String filename) throws Exception {
    Object[] obArr = p.toArray();
    ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
    for (Object ob : obArr) {
    out.writeObject(ob);
    }
    out.close();
    }
    If you wanna get object from this file, you can use ObjectInputStream

Similar Threads

  1. Replies: 7
    Last Post: 08-22-2010, 12:51 PM
  2. Replies: 8
    Last Post: 02-18-2010, 01:54 PM
  3. Saving ArrayList to File
    By nwboy74 in forum Advanced Java
    Replies: 6
    Last Post: 12-08-2008, 11:19 AM
  4. Replies: 0
    Last Post: 04-04-2008, 03:48 PM
  5. Replies: 0
    Last Post: 07-16-2007, 09:45 PM

Posting Permissions

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