Results 1 to 10 of 10

Thread: Quick Java Help

  1. #1
    cat /dev/null streetster's Avatar
    Join Date
    Jul 2003
    Location
    London
    Posts
    4,138
    Thanks
    119
    Thanked
    100 times in 82 posts
    • streetster's system
      • Motherboard:
      • Asus P7P55D-E
      • CPU:
      • Intel i5 750 2.67 @ 4.0Ghz
      • Memory:
      • 4GB Corsair XMS DDR3
      • Storage:
      • 2x1TB Drives [RAID0]
      • Graphics card(s):
      • 2xSapphire HD 4870 512MB CrossFireX
      • PSU:
      • Corsair HX520W
      • Case:
      • Coolermaster Black Widow
      • Operating System:
      • Windows 7 x64
      • Monitor(s):
      • DELL U2311
      • Internet:
      • Virgin 50Mb

    Quick Java Help

    Hey Guys,

    the whole java Serializable thing... will this let me save a load of objects into a file so that they can be read back later?

    I basically want to be able to create 'projects' within my program, so the ability to save and load projects would be fairly useful... but I dont know the best way to go about this... is there an alternative? If you need any more info just ask

  2. #2
    Senior Member Shad's Avatar
    Join Date
    Jul 2003
    Location
    In front
    Posts
    2,773
    Thanks
    22
    Thanked
    40 times in 24 posts
    Quote Originally Posted by streetster View Post
    the whole java Serializable thing... will this let me save a load of objects into a file so that they can be read back later?
    Basically, yes. That's what serializing stuff does - converts your objects into a big long string which can be converted back into the object in its original state.

    Might want to consider XML for your 'projects' though.
    Simon


  3. #3
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,164
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts
    serialization is the proccess of converting an object to a (think tape) 'stream' like format.

    Its a bit too much of a buzz word because it allows for remoting (ie, using an object on a remote PC).

    Some types can't be easily serialised, but that is another story.

    In short, its great
    throw new ArgumentException (String, String, Exception)

  4. #4
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    As shad said you may want to consider XML if you can rebuild your objects from simple (and sometimes not so simple) parameters.
    To err is human. To really foul things up ... you need a computer.

  5. #5
    cat /dev/null streetster's Avatar
    Join Date
    Jul 2003
    Location
    London
    Posts
    4,138
    Thanks
    119
    Thanked
    100 times in 82 posts
    • streetster's system
      • Motherboard:
      • Asus P7P55D-E
      • CPU:
      • Intel i5 750 2.67 @ 4.0Ghz
      • Memory:
      • 4GB Corsair XMS DDR3
      • Storage:
      • 2x1TB Drives [RAID0]
      • Graphics card(s):
      • 2xSapphire HD 4870 512MB CrossFireX
      • PSU:
      • Corsair HX520W
      • Case:
      • Coolermaster Black Widow
      • Operating System:
      • Windows 7 x64
      • Monitor(s):
      • DELL U2311
      • Internet:
      • Virgin 50Mb
    hmmm... i may look into xml but that sounds like its going to involve some effort making an xml parser etc, whereas if i were to serialize (hate that z!) the objects then that may be an easier option

  6. #6
    Senior Member Shad's Avatar
    Join Date
    Jul 2003
    Location
    In front
    Posts
    2,773
    Thanks
    22
    Thanked
    40 times in 24 posts
    If it's an internal 'file format' then I don't think you need to go mad. Assuming Java has an XML 'engine' like .NET has, just [try to] navigate directly to the nodes you need. It's about 4 lines of code in .NET and I was guessing it would be similarly simple in Java, that's why I suggested it as a decent alternative to serializing.

    I don't think serialization is the right solution to your problem. It's a bit of a sledgehammer-to-crack-an-egg, kind of thing
    Simon


  7. #7
    cat /dev/null streetster's Avatar
    Join Date
    Jul 2003
    Location
    London
    Posts
    4,138
    Thanks
    119
    Thanked
    100 times in 82 posts
    • streetster's system
      • Motherboard:
      • Asus P7P55D-E
      • CPU:
      • Intel i5 750 2.67 @ 4.0Ghz
      • Memory:
      • 4GB Corsair XMS DDR3
      • Storage:
      • 2x1TB Drives [RAID0]
      • Graphics card(s):
      • 2xSapphire HD 4870 512MB CrossFireX
      • PSU:
      • Corsair HX520W
      • Case:
      • Coolermaster Black Widow
      • Operating System:
      • Windows 7 x64
      • Monitor(s):
      • DELL U2311
      • Internet:
      • Virgin 50Mb
    so far each 'Project' has a String description and an array of Modules, probably with some other stuff thrown in if i need it.

    each module has a String description and then 3 int values...

    Code:
    <project>
     <module_array>
       <[0]>
           <description>"string here"</description>
           <int1>100</int1>
           <int2>200</int2>
           <int3>300</int3>
       </[0]>
       <[1]> ....... </[1]>
     <module_array>
    </project>
    ?

  8. #8
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    Implementing XML parsers is easy in almost any language. The work the W3C has done to produce standards and IDL is quite amazing. To give you an idea for your XML implementation however, here's an example XML save-file for a vector graphics based app I once made:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE sketch SYSTEM "simplepaint.dtd">
     <canvas>
      <circle angle="0.0" radius="48.104053883222775">
       <color B="255" G="0" R="0"/>
       <position x="106.0" y="27.0"/>
      </circle>
      <circle angle="0.0" radius="70.21395872616783">
       <color B="255" G="0" R="0"/>
       <position x="246.0" y="7.0"/>
      </circle>
      <circle angle="0.0" radius="18.027756377319946">
       <color B="0" G="0" R="255"/>
       <position x="197.0" y="158.0"/>
      </circle>
      <text angle="0.0">
       <color B="0" G="255" R="0"/>
       <position x="447.0" y="149.0"/>
       <font fontname="SansSerif" fontstyle="plain" pointsize="12"/>
       <string>
        <bounds height="12" width="80"/>
        My Names Ally
       </string>
      </text>
      <text angle="0.0">
       <color B="0" G="255" R="0"/>
       <position x="427.0" y="291.0"/>
       <font fontname="Book Antiqua" fontstyle="bold-italic" pointsize="22"/>
       <string>
        <bounds height="18" width="69"/>
        I smell
       </string>
      </text>
      <circle angle="0.0" radius="183.50476833041697">
       <color B="255" G="0" R="0"/>
       <position x="60.0" y="-20.0"/>
      </circle>
     </canvas>
    Here is the dtd:

    Code:
    <!ELEMENT canvas (line|circle|rectangle|curve|text)*>
    
    <!ELEMENT color EMPTY>
       <!ATTLIST color 
                 R CDATA                   #REQUIRED
                 G CDATA                   #REQUIRED
                 B CDATA                   #REQUIRED
       >
    
    <!ENTITY % coordinates "x CDATA #REQUIRED y CDATA #REQUIRED">
    
    <!ELEMENT position EMPTY>
       <!ATTLIST position %coordinates;> 
    
    <!ELEMENT endpoint EMPTY>
       <!ATTLIST endpoint %coordinates;>
    
    <!ELEMENT line (color, position, endpoint)>
       <!ATTLIST line 
                 angle CDATA                   #REQUIRED
       >
    
    <!ELEMENT rectangle (color, position, bottomright)>
       <!ATTLIST rectangle
                 angle CDATA                   #REQUIRED
       >
    
    <!ELEMENT bottomright EMPTY>
       <!ATTLIST bottomright %coordinates;>
    
    <!ELEMENT circle (color, position)>
       <!ATTLIST circle
                 radius CDATA                  #REQUIRED
                 angle CDATA                   #REQUIRED
       >
    
    <!ELEMENT curve (color, position, point+)>
       <!ATTLIST curve  angle CDATA  #REQUIRED>
    
    <!ELEMENT point EMPTY>
       <!ATTLIST point %coordinates;>
    
    <!ELEMENT text (color, position, font, string)>
       <!ATTLIST text  angle CDATA  #REQUIRED>
    
    <!ELEMENT font EMPTY>
       <!ATTLIST font
                 fontname  CDATA                           #REQUIRED
                 fontstyle (plain|bold|italic|bold-italic) #REQUIRED
                 pointsize CDATA                           #REQUIRED
       >
    
    <!ELEMENT string (#PCDATA|bounds)*>
    
    <!ELEMENT bounds EMPTY>
       <!ATTLIST bounds 
                 width     CDATA               #REQUIRED
                 height    CDATA               #REQUIRED 
       >
    Here is the alternative XML Schema:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    	<xsd:element name="canvas" type="SketchType"/>
    	
    	<!--Type for a sketch root element -->
    	<xsd:complexType name="SketchType">
    		<xsd:choice minOccurs="0" maxOccurs="unbounded">
    			<xsd:element name="line" type="LineType" />
    			<xsd:element name="rectangle" type="RectangleType" />
    			<xsd:element name="circle" type="CircleType" />
    			<xsd:element name="curve" type="CurveType" />
    			<xsd:element name="text" type="TextType" />
    		</xsd:choice>
    	</xsd:complexType>
    	
    	<!--Type for a line element -->
    	<xsd:complexType name="LineType">
    		<xsd:sequence>
    			<xsd:element name="color" type="ColorType" />
    			<xsd:element name="position" type="PointType" />
    			<xsd:element name="endpoint" type="PointType" />
    		</xsd:sequence>
    		<xsd:attribute name="angle" type="xsd:double" />
    	</xsd:complexType>
    	
    	<!--Type for a rectangle element -->
    	<xsd:complexType name="RectangleType">
    		<xsd:sequence>
    			<xsd:element name="color" type="ColorType" />
    			<xsd:element name="position" type="PointType" />
    			<xsd:element name="bottomright" type="PointType" />
    		</xsd:sequence>
    		<xsd:attribute name="angle" type="xsd:double" />
    	</xsd:complexType>
    	
    	<!--Type for a circle element -->
    	<xsd:complexType name="CircleType">
    		<xsd:sequence>
    			<xsd:element name="color" type="ColorType" />
    			<xsd:element name="position" type="PointType" />
    		</xsd:sequence>
    		<xsd:attribute name="radius" type="xsd:double" />
    		<xsd:attribute name="angle" type="xsd:double" />
    	</xsd:complexType>
    	
    	<!--Type for a curve element -->
    	<xsd:complexType name="CurveType">
    		<xsd:sequence>
    			<xsd:element name="color" type="ColorType" />
    			<xsd:element name="position" type="PointType" />
    			<xsd:element name="point" type="PointType" minOccurs="1" maxOccurs="unbounded" />
    		</xsd:sequence>
    		<xsd:attribute name="angle" type="xsd:double" />
    	</xsd:complexType>
    	
    	<!--Type for a text element -->
    	<xsd:complexType name="TextType">
    		<xsd:sequence>
    			<xsd:element name="color" type="ColorType" />
    			<xsd:element name="position" type="PointType" />
    			<xsd:element name="font" type="FontType" />
    			<xsd:element name="string" type="xsd:string" />
    		</xsd:sequence>
    		<xsd:attribute name="angle" type="xsd:double" />
    	</xsd:complexType>
    	
    	<!--Type for a font element -->
    	<xsd:complexType name="FontType">
    		<xsd:attribute name="fontname" type="xsd:string" use="required" />
    		<xsd:attribute name="fontstyle" use="required">
    			<xsd:simpleType>
    				<xsd:restriction base="xsd:string">
    					<xsd:enumeration value="plain" />
    					<xsd:enumeration value="bold" />
    					<xsd:enumeration value="italic" />
    					<xsd:enumeration value="bold-italic" />
    				</xsd:restriction>
    			</xsd:simpleType>
    		</xsd:attribute>
    	</xsd:complexType>
    	
    	<!--Type for elements representing points -->
    	<xsd:complexType name="PointType">
    		<xsd:attributeGroup ref="coords" />
    	</xsd:complexType>
    	
    	<!--Type for a color element -->
    	<xsd:complexType name="ColorType">
    		<xsd:attribute name="R" type="xsd:nonNegativeInteger" use="required" />
    		<xsd:attribute name="G" type="xsd:nonNegativeInteger" use="required" />
    		<xsd:attribute name="B" type="xsd:nonNegativeInteger" use="required" />
    	</xsd:complexType>
    	
    	<!--Attribute group specifying point coordinates -->
    	<xsd:attributeGroup name="coords">
    		<xsd:attribute name="x" type="xsd:double" use="required" />
    		<xsd:attribute name="y" type="xsd:double" use="required" />
    	</xsd:attributeGroup>
    </xsd:schema>
    Personally i'm not 100% comfortable with Schemas and even the above with its extremely simple namespace can become confusing, probably because I started with dtd's and am stuck in my ways.

    But still you can see how the XML file above could be loaded by the parser (using either SAX or DOM in Java, I prefer SAX for most of my work, default parser is xerces in recent versions I believe) and then the attributes of each node can be thrown off to their respective original methods to recreate the graphics on the canvas.
    To err is human. To really foul things up ... you need a computer.

  9. #9
    Senior Member
    Join Date
    Aug 2005
    Posts
    213
    Thanks
    0
    Thanked
    0 times in 0 posts
    Be careful of serialization when you change the interface and face Serial uid issues. Each object has a unique serial uid (should do anyway) which is either defined by yourself in your class, or the VM does it based on your objects interface. If you change the class and tried to read an old object stream based on the old definition you will encounter serial uid issues.

    One thing you may want to look at is externatlization which lets you serialize your object but provides an interface for you to code the reading and writing of the object.

  10. #10
    cat /dev/null streetster's Avatar
    Join Date
    Jul 2003
    Location
    London
    Posts
    4,138
    Thanks
    119
    Thanked
    100 times in 82 posts
    • streetster's system
      • Motherboard:
      • Asus P7P55D-E
      • CPU:
      • Intel i5 750 2.67 @ 4.0Ghz
      • Memory:
      • 4GB Corsair XMS DDR3
      • Storage:
      • 2x1TB Drives [RAID0]
      • Graphics card(s):
      • 2xSapphire HD 4870 512MB CrossFireX
      • PSU:
      • Corsair HX520W
      • Case:
      • Coolermaster Black Widow
      • Operating System:
      • Windows 7 x64
      • Monitor(s):
      • DELL U2311
      • Internet:
      • Virgin 50Mb
    the xml route looks a bit nicer, guess i shall create some toXML() methods for my stuff

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Key Java source code to be opened
    By Steve in forum HEXUS News
    Replies: 0
    Last Post: 27-06-2005, 10:54 AM
  2. Replies: 0
    Last Post: 14-06-2005, 12:02 PM
  3. ATi + Java + BSOD?
    By Kezzer in forum PC Hardware and Components
    Replies: 1
    Last Post: 11-06-2005, 01:26 PM
  4. Potential HUGE source of free Java games / apps
    By PriestJPN in forum Smartphones and Tablets
    Replies: 4
    Last Post: 03-11-2004, 01:59 AM
  5. Firefox (Moz/Netscape too) Quick Searches
    By Zathras in forum Software
    Replies: 2
    Last Post: 24-08-2004, 04:50 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
  •