want to read a nested ArrayList from a text file.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: want to read a nested ArrayList from a text file.

  1. #1
    Join Date
    Aug 2007
    Posts
    4

    want to read a nested ArrayList from a text file.

    Hi,
    I have a nested java ArrayList in a text file. I need to read that ArrayList in a Java program, and work with it. The problem is, whenever I am reading the file, I could only read the ArrayList as a String. Can anyone please help me to read the List in the file, as the same list in the java program?

    Actually, I have created a java Arraylist in another program, and have copied the arraylist to a text file. The Arraylist is nested: (e.g. [a, b, [c, [d, e]]] ) Now, I need to read that ArrayList from the text in a java program and work with it. The problem I am facing is, I can read the ArrayList only as a string from the file. I am using :

    " FileReader fr = new FileReader ("javaList.txt");
    BufferedReader br = new BufferedReader (fr);"

    Now, converting that string to a similar nested Arraylist is proving to be difficult for me. I don't know how to do it.

    Thanks a lot!

  2. #2
    Join Date
    Mar 2004
    Posts
    635
    Was the arraylist written to file as a serialized object or just plain text? If plain text, then what structure was it written? Just reverse the method you used for writing to read it back in.

  3. #3
    Join Date
    Aug 2007
    Posts
    4
    Hi, i read a python List and converted it to a Java list in Jython. This is the code in Jython :

    input=open("pythonList.txt",'r')
    fileList=input.readline()
    input.close()

    p = listquote.LineParser() // read the python list from the file as a list.
    aList = p.feed(fileList)
    anotherJavaList= java.util.LinkedList(aJavaList)

    output = open('javaList.txt','w')
    print >> output, anotherJavaList
    output.close()

    Now, I need to read that List from javaList.txt into a Java program, and work with that.
    Thanks!

  4. #4
    Join Date
    Mar 2004
    Posts
    635
    You may just have to save the list in a format you can easily parse so it can be recreated. You may want to consider saving the file in XML format.
    Just as an example:
    Code:
    <container id="foo">
        <item>thing1</item>
        <item>thing2</item>
        <container id="stuff">
            <item>stuff</item>
        </container>
    </container>
    <container id="bar">
        <item>thing1</item>
    </container>

Similar Threads

  1. Replies: 1
    Last Post: 12-13-2005, 03:36 AM
  2. How to read all words in a text file
    By crazyguitarchik in forum Java
    Replies: 1
    Last Post: 11-30-2005, 08:12 PM
  3. Can a text file be read using ADO?
    By Ted Young in forum VB Classic
    Replies: 1
    Last Post: 08-21-2000, 09:47 PM
  4. Replies: 1
    Last Post: 05-03-2000, 03:38 AM
  5. Replies: 0
    Last Post: 04-17-2000, 01:33 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center