Write Objects to file using Externalizable


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Write Objects to file using Externalizable

  1. #1
    Join Date
    Jun 2005
    Posts
    3

    Write Objects to file using Externalizable

    Hi,

    I'm trying to write an object to file. My sample code is:

    public class Junk implements Externalizable{
    private static java.util.Random generator = new java.util.Random();
    private int answer;
    private double[] numbers;
    private String thought;

    public Junk(String thought) {
    this.thought = thought;
    answer = 42;

    numbers = new double[3+ generator.nextInt(4)];
    for (int i=0; i<numbers.length; i++) {
    numbers[i] = generator.nextDouble();
    }
    }


    public void writeExternal(ObjectOutput stream) throws java.io.IOException {
    stream.writeInt(answer);
    stream.writeBytes(thought);
    for(int i=0; i< numbers.length; i++) {
    stream.writeDouble(numbers[i]);
    }
    }

    public void readExternal(ObjectInput stream) throws java.io.IOException {
    answer = stream.readInt();
    String thought = stream.readUTF();
    }
    }



    and the class with main() is:




    package MyTest;

    import java.io.*;

    public class SerializeObjects {
    public SerializeObjects() {
    }

    public static void main(String args[]) {
    Junk obj1 = new Junk("A green twig is easily bent.");
    Junk obj2 = new Junk("A little knowledge is a dangerous thing.");
    Junk obj3 = new Junk("Flies light on lean horses.");

    ObjectOutputStream oOut = null;
    FileOutputStream fOut = null;

    try {
    fOut = new FileOutputStream("E:\\FileTest\\test.bin");
    oOut = new ObjectOutputStream(fOut);
    obj1.writeExternal(oOut);
    //obj2.writeExternal(oOut);
    } catch (IOException e) {
    e.printStackTrace(System.err);
    System.exit(1);
    }

    try {
    oOut.flush();
    oOut.close();
    fOut.close();
    } catch(IOException e) {
    e.printStackTrace(System.err);
    System.exit(1);
    }
    }
    }

    The output I get in test.bin contains some junk ascii codes. The only item that is written correctly in the file is the string.
    Is there anyway I can write correct data into a file?
    My output needs to be a readable text format file.

    Can anyone help please?

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Then you should use a plain BufferedWriter. Serialized/Externalized data not is
    intended for human eyes, as your output is (?)
    eschew obfuscation

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