Read arbitrary data type from file

edited February 2014 in Programming Questions

(My first-ever post on the Processing forum, whoopdy-doo. Hope I'm not too noob for you guys.)

I'm making a program to hold arbitrarily-nested lists; i.e. I have a list of things, and each of those things may or may not contain a list of more things of the same type, etc. Not only that, but I have more than one such list of lists of arbitrarily nested lists, and I want to be able to pick one of them to deal with at a time. Thus, I need the functionality of saving and loading to and from a file.

I can write to files and read from files just fine, but only with alphanumeric text. Rather than convert the list (and the sub-lists, oh dearie me) to text and then convert it back from text when I load it, is there some way to read in something other than a string from a file? I want to just "save this" and then later "read this".

I've done some Googling, but the only things I can find are for Strings and maybe integers, but not anything else. BufferedReader doesn't do what I want (as far as I can tell?), loadStrings doesn't do what I want, blar.

Thanks in advance for any advice!

Answers

  • edited February 2014 Answer ✓

    Base Java serialization has an issue: if you change ever so lightly the saved class, you will no longer be able to read it back...

    If that's not an issue, it might be the simplest solution, working out of the box.

    For more long-term solution, better use a more robust solution, using, for example, a text format, XML or Json for example.
    Having some experience with it, I would use the Jackson library to save to and read from Json. But something like XStream is also very flexible.

  • So, I went and tried out XStream, and got it working. I can make it write integers and stuff to a file in XML format just fine, but when I try to give an object with nested ArrayLists it pukes up a bunch of... of I don't know what. Is it the Java language made manifest? Whatever, here, have a sample.

    >false</alignOnBaseline><hgap>5</hgap><newAlign>1</newAlign><serialVersionOnStream>1</serialVersionOnStream><vgap>5</vgap></default></java.awt.FlowLayout></layoutMgr><dispatcher><nativeContainer class="NestListButton" reference="../../../.."></nativeContainer><eventMask>0</eventMask></dispatcher><focusCycleRoot>false</focusCycleRoot><containerSerializedDataVersion>1</containerSerializedDataVersion><focusTraversalPolicyProvider>false</focusTraversalPolicyProvider></default><null></null><null></null></java.awt.Container><java.applet.Applet><default></default></java.applet.Applet><processing.core.PApplet><default><cursorType>0</cursorType><cursorVisible>true</cursorVisible><defaultSize>false</defaultSize><displayHeight>1080</displayHeight><displayWidth>1920</displayWidth><dmouseX>393</dmouseX><dmouseY>140</dmouseY><emouseX>393</emouseX><emouseY>140</emouseY><exitCalled>true</exitCalled><external>true</external><finished>true</finished><firstMouse>false</firstMouse><focused>false</focused><frameCount>61</frameCount><frameRate>59.97107</frameRate><frameRateLastNanos>423196996583574</frameRateLastNanos><frameRatePeriod>16666666</frameRatePeriod><frameRateTarget>60.0</frameRateTarget><height>640</height><insideDraw>true</insideDraw><key></key><keyCode>0</keyCode><keyPressed>false</keyPressed><looping>true</looping><millisOffset>1391476711529</millisOffset><mouseButton>37</mouseButton><mousePressed>false</mousePressed><mouseX>393</mouseX><mouseY>140</mouseY><online>false</online><paused>false</paused><perlin__PI>0</perlin__PI><perlin__TWOPI>0</perlin__TWOPI><perlin__amp__falloff>0.5</perlin__amp__falloff><perlin__octaves>4</perlin__octaves><pmouseX>393</pmouseX><pmouseY>140</pmouseY><redraw>false</redraw><requestImageCount>0</requestImageCount>

    Apparently XStream doesn't like nested ArrayLists. Or maybe I just set it up wrong?

    Well, worst case scenario, I write my own parser and I learned about serialization, so thanks regardless XD

  • What you show is called XML... That's the format XStream uses to save data! Or do I miss something?

  • edited February 2014

    Oh, it's XML alright, but- and this is a really big "but"- it does not contain the information I'm trying to store. Look at some of the things it's got it there:<emousex>, <frameRate>, <focusCycleRoot>. I don't even know what half of these things are. What is/are <perlin_octaves> and why are there 4 of them?

    Compare this with the example XML from XStream's two-minute tutorial page.

    <person> <firstname>Joe</firstname> <lastname>Walnes</lastname> <phone> <code>123</code> <number>1234-456</number> </phone> <fax> <code>123</code> <number>9999-999</number> </fax> </person>

    I quite clearly do not have what I should. :/

    EDIT: I don't seem to have mentioned in my previous post; this XML file is 12MB in size. And the last XML tag doesn't have its closing "greater than" character. This is to store five lists, three of which contain one or two objects, and two of which are empty.

  • Without code, without knowing what you are saving, it is hard to help. It looks like you are saving 'this', ie. the whole sketch content, with PApplet and all public data, or something like that.

Sign In or Register to comment.