Writing Objects

Hi all,
I am writing an Object to a data file and I have the following logic:
for(int i=0; i<250;i++)
for(int j=0; j< 3;j++)
out.writeObject(A1[i][j]);
out.close();
I am writing 3 columns and 250 rows. I want to have a tab space seperating each column. How do I add a tab space after writing each column.
Thanks.

1) Objects in the Array
The first column is text, second comulmn is an Integer, third column is text again.
2) Yes, I need to write a text file.
3) Here is the format:
Mathematics 45 Pass
Physics 35 Fail
and so on. Again I need a Tab space between each Column. I guess something is wrong with the way I am using the WriteObject() method because I took out the line thats writing a Tab space and the program
still writes junk to te text file. The below is what is there in the text file.
�� ur [[Ljava.lang.Object;��S�k��  xp   �ur [Ljava.lang.Object;��X�s)l  xp   t 1t 2puq ~    t 1t 3t 3uq ~    ppt 2uq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    pppuq ~    ppp                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

Similar Messages

  • Writing Objects to File

    I have a number of records contained in a vector. Having trouble writing these records to a file. Even trying to write one record, as below, is throwing an exception. The Student class implements serializable and the method main throws IOException
    public  void saveRecords()
              try
                   FileOutputStream outStream = new FileOutputStream ("StudentRecord.dat");
                   ObjectOutputStream objOutStream = new ObjectOutputStream (outStream);
                   objOutStream.writeObject (student.elementAt(1));
                   objOutStream.flush();
                   objOutStream.close();
              } // end try
              catch (Throwable e)
                   System.out.println ("Error Writing File");
              } // end catch
         } // end saveRecords                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    Thanks.
    I've sorted out the write part. The problem was that a student record object consisted objects of other classes, Name, Address, Course etc. All classes had to implement serializable before I could write the student objects to a file.
    Now having trouble with the reloading though. I've input 3 records which were successfully saved. However, on attempting to load these, I get an EOFexception. If I subsequently perform an enquiry on the records after attempting the read operation, records 1 and 3 are accessible but not record 2. Why should I get an EOFexception after 2 of 3 records Here's the code.
    public  void loadRecords ()
              try
                   FileInputStream inStream = new FileInputStream ("StudentRecord1.dat");
                   ObjectInputStream objInStream = new ObjectInputStream (inStream);
                   do
                   student.addElement((PGStudent)objInStream.readObject());
                             } while (objInStream.readObject() != null);
              } // end try
              catch (Throwable e)
                   System.out.println ("Error Reading File" + e.toString());
              } //  end catch
         } // end loadRecords
         public  void saveRecords()
              try
                   FileOutputStream outStream = new FileOutputStream ("StudentRecord1.dat");
                   ObjectOutputStream objOutStream = new ObjectOutputStream (outStream);
                   Enumeration enum = student.elements();
                   while(enum.hasMoreElements())
                        objOutStream.writeObject (enum.nextElement());
                   objOutStream.flush();
                   objOutStream.close();
              } // end try
              catch (Throwable e)
                   System.out.println ("Error Writing File" + e.toString());
              } // end catch
         } // end saveRecords

  • Problem writing object to file

    Hi everyone,
    I am creating an index by processing text files. No of files are 15000 and index is a B+ Tree. when all files processed and i tried to write it to the file it gives me these errors.
    15000 files processed.
    writing to disk...
    Exception in thread "main" java.lang.StackOverflowError
            at sun.misc.SoftCache.processQueue(SoftCache.java:153)
            at sun.misc.SoftCache.get(SoftCache.java:269)
            at java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:244)
            at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1029)
            at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1369)
            at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:380)
            at java.util.Vector.writeObject(Vector.java:1018)
            at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:585)
            at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:890)
            at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1333)
            at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284)
            at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073)
            at java.io.ObjectOutputStream.writeArray(ObjectOutputStream.java:1245)
            at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1069)
            at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1369)
            at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:380)
            at java.util.Vector.writeObject(Vector.java:1018)
            at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:585)
            at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:890)
            at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1333)
            at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284)
            at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073)
            at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1369)
            at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1341)
            at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284)
            at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073)
            at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1369)
            at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1341)
            at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284)
            at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073)
            at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1369)
            at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1341)
            at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284)
            at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073)
            at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1369)
            at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1341)
            at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284)
            at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073)
            at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1369)
            at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1341)
            at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1284)
            at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1073)
            at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1369)
    ..........can anyone point out the mistake im doing?
    thanks

    the B+ Tree is balanced and is perfectly working without writing to the file. and i am using default writeObject() method of ObjectOutputStream.
      try {
                                FileOutputStream   f_out   = new   FileOutputStream ("tree.idx");
                                ObjectOutputStream obj_out = new   ObjectOutputStream (new BufferedOutputStream (f_out));   
         for (int x = 0, l = files.length; x < l; x++) {
              ProcessTree(files[x].toString());
                    System.out.println("Writing main index to the disk...");
                    obj_out.writeObject (tree); 
                    obj_out.flush();
                    obj_out.close();
                    } catch (Exception e)
          System.out.println (e.toString ());
          System.out.println("Error Writing to Disk!");
        }

  • Writing objects with in zip files

    Hi,
    I am trying to write an object into a zip file :
    ZipOutputStream zout=new ZipOutputStream(new FileOutputStream("test.zip");
    ZipEntry ze=new ZipEntry("test.dat");
    zout.putNextEntry(ze);
    ObjectOutputStream out=new ObjectOutputStream(zout);
    out.writeObject(obj);
    out.close();
    zout.closeEntry();
    zout.close();
    The above code is within a try-catch block.
    It is compiling correctly.
    But it is throwing an exception just after executing "zout.closeEntry()".
    The exception is "java.io.Exception: Stream Closed".
    I am using JDK 1.3.
    Can anyone help me.
    Indra.

    Hi Indra,
    I'm sorry it didn't work. I would try to simulate that problem but I really don't have time right now (but I will).
    What you can do is to try to create that zip file in 2 steps, first just serialize the objects in a ordinary file and then read from that file and create a zip file. Of course you'll have to do the same to get your objects out from the zip file.
    If the file is not too big you can use a ByteArrayOutputStream instead of a FileOutputStream for the first file.
    hope it will work,
    Gabe

  • Writing object to file

    My dilemma is i am wanting to write an arraylist to a specific folder within the package mounted in Netbeans. My only problem is I am running on a mac and cannot get to write to correct place. I have tried to use /.../file name etc. It works if i explicitly give the whole path but i do not want to do this as this app will need to transportable to other machines. Any help please.
    Thanks
    Chad

    I tried you suggestion and it works good for getting to the users home folder but I am wanting to contain the resource data file all within the same package. ie... Folder contains all .java files and any resource folders needed. I can get the resource just fine using getClass().getResourceAsStream() but when i write out i get errors. I am using
    Is there something like the getClass... that mimics writing out to files. I am also using a mac and only plan on distributing this to other macs only.
    Thanks
    Chad

  • Writing 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);
    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?

    obj1.writeExternal(oOut);This should be
    oOut.writeObject(obj1);However,
    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.If you don't want 'junk' don't use Externalizable and ObjectOutputStream at all, just use PrintStream/PrintWriter.println().

  • Error in writing object to database

    Hi,
    When I go to "persistent.xml-><EJBname>->cmr-field-><fieldName>", on right hand side, in additional information section it shows "ERROR:
    The class of this dependent value has no public fields. Because of this it can only be mapped to a single column".
    That field is an object of some other class. So, whereever I have any column storing object of some class, I have the same message.
    Due to his, its giving deployment error.
    "Cannot obtain an appropriate JDBC type for class com.bea.medrec.entities.RecordLocal. To store the field with this class in the database, the class must implement java.io.Serializable."
    Please suggest what could be the solution.
    Thanks!

    Hi Dhamandheep ,
    which is the data type of the field u have  created?.
    is it your own  class type (Custom class).
    if u r using your own class as data type , u have to implement java.io.Serializable to that class.
                                        regards
                                        Kishor Gopinathan

  • Help me to solve writing object to file ?

    what's the happen ? when I write object to file ?
    My error is : java.io.NotSerializableException : Sun.awt.image.ToolkitImage
    What do I do to expect this error ?
    Please help me
    Thanks Very much

    you need to use ObjectInputStream and ObjectOutput Stream, you can write to file or read from file through readObject and write object.
    try this..!!

  • Writing objects to a transport request

    Hi all,
    I generate structures and table types in an ABAP Program and I want to record these objects to a transport request. I use the function modules 'TR_OBJECTS_CHECK' and 'TR_OBJECTS_INSERT'  to write the objects to a transport request, but in this case, for each object the dialog for entering the package name and transport request is shown. I want that all objects are saved in the same package and transport requests. Is there another function module by which I can record all generated objects in a transport request after the request and package number is asked only once?
    Regards,
    Sükrü

    Hi Suekrue,
    I think 'TR_APPEND_TO_COMM' is the one. We did something similar. First choose/Create the Request, then append objects:
    *&      Form  TR_REQUEST_CHOICE
    FORM tr_request_choice  CHANGING pv_trkorr TYPE trkorr.
      TYPE-POOLS:
        trwbo. "complex types for transport request display
      DATA:
        ls_request                            TYPE trwbo_request_header.
      CALL FUNCTION 'TR_REQUEST_CHOICE'
        EXPORTING
    *   IV_SUPPRESS_DIALOG                    = ' '
    *   IV_REQUEST_TYPES                      =
    *   IV_CLI_DEP                            = ' '
    *   IV_REQUEST                            = ' '
    *   IT_E071                               =
    *   IT_E071K                              =
    *   IV_LOCK_OBJECTS                       = ' '
          iv_title                            = 'Auftrag auswählen, Aufgabe wird gefunden'
    *   IV_START_COLUMN                       = 3
    *   IV_START_ROW                          = 7
    *   IV_WITH_ERROR_LOG                     = 'X'
    *   IV_NO_OWNER_CHECK                     = ' '
        IMPORTING
          es_request                          = ls_request
        EXCEPTIONS
          invalid_request                     = 1
          invalid_request_type                = 2
          user_not_owner                      = 3
          no_objects_appended                 = 4
          enqueue_error                       = 5
          cancelled_by_user                   = 6
          recursive_call                      = 7
          OTHERS                              = 8
      IF sy-subrc                             = 0.
        SELECT trkorr
          INTO pv_trkorr UP TO 1 ROWS
          FROM e070
          WHERE trfunction = 'S'
            AND trstatus = 'D'
            AND as4user = sy-uname
            AND strkorr = ls_request-trkorr.
          EXIT.
        ENDSELECT."rkorr into pv_trkorr
      ENDIF.
    ENDFORM.                    " TR_REQUEST_CHOICE
    *&      Form  TR_APPEND_TO_COMM
    FORM tr_append_to_comm
      CHANGING ps_alv                         TYPE LINE OF ty_t_alv.
      DATA:
        ls_e071                               TYPE e071,
        lv.
      ls_e071-pgmid                           = 'LIMU'.
      ls_e071-object                          = 'FUNC'.
      ls_e071-obj_name                        = ps_alv-funcname.
      CALL FUNCTION 'TR_APPEND_TO_COMM'
        EXPORTING
          pi_korrnum                          = p_trkorr
          wi_e071                             = ls_e071
          wi_simulation                       = p_test
    *   WI_SUPPRESS_KEY_CHECK                 = ' '
    * TABLES
    *   WT_E071K                              =
        EXCEPTIONS
          no_authorization                    = 1
          no_systemname                       = 2
          no_systemtype                       = 3
          tr_check_keysyntax_error            = 4
          tr_check_obj_error                  = 5
          tr_enqueue_failed                   = 6
          tr_ill_korrnum                      = 7
          tr_key_without_header               = 8
          tr_lockmod_failed                   = 9
          tr_lock_enqueue_failed              = 10
          tr_modif_only_in_modif_order        = 11
          tr_not_owner                        = 12
          tr_no_append_of_corr_entry          = 13
          tr_no_append_of_c_member            = 14
          tr_no_shared_repairs                = 15
          tr_order_not_exist                  = 16
          tr_order_released                   = 17
          tr_order_update_error               = 18
          tr_repair_only_in_repair_order      = 19
          tr_wrong_order_type                 = 20
          wrong_client                        = 21
          OTHERS                              = 22
      IF sy-subrc                             <> 0.
        MESSAGE ID sy-msgid                   TYPE sy-msgty NUMBER sy-msgno
                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
                INTO ps_alv-tr_message.
      ENDIF.
    ENDFORM.                    " TR_APPEND_TO_COMM
    Pleas adapt to your needs.
    Regards,
    Clemens

  • Reading/Writing Objects from/to Sockets

    I have a list of objects on the Server side that I want to write to a Client. The code on the Server side looks like this
    Socket socket = serverSocket.accept( )
    ObjectOutputStream oos = new ObjectOutputStream( socket.getOutputStream());
    for ( int i = 0 ; i < list.size ( ) ; i++)
    oos.writeObject(list.get ( i ) ;
    }On the Client side it looks like this
    ServerSocket socket = new ServerSocket( 7000 )
    socket.accept ( )
    while( true )
    ObjectInputStream ois = new ObjectInputStream( socket.getInputStream( ) )
    Object obj = ois.readObject( )
    }My question is; how to "mark" on the server side that I written all the objects, so that I know on the client side when to end reading objects ?
    When dealing with bytes, to mark EOF one can do:
    socket.write(new byte[1])on the server side
    And on the client side
    while( socket.read != -1 )to know when to quit reading.

    You could just pass an EndObject that marks the end (e.g Byte or an own Object).
    But I don't think you need this. Most collections are Serializable so you can just pass your list to the stream.

  • NIO and writing objects

    I was curious if anyone has found a way to write an object to a ByteBuffer besides doing this:
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(baos);
    out.writeObject(new Object());
    ByteBuffer buf = ByteBuffer.wrap(baos.toByteArray());Also, does anyone know a good way to rebuild an object that was streamed using a Non-blocking SocketChannel?
    Thanks,
    b2s

    You have found the way. If you already have a ByteBuffer you can also just put() the ByteArrayOutputStream.getBytes() into it.
    You generally need to prepend the length of the array containing the object so you know when reading that you have the entire object. Reading it is then the reverse: read the length, read that many bytes, put them into a ByteArrayInputStream, then wrap an ObjectInputStream round that and do readObject().

  • Writing objects to standard output

    Hi,
    I am trying to write an object to standard output but i am not getting a proper result. Here's my code
    import java.io.*;
    public class Stutest extends Person implements Serializable {
    public String bday,email,major;
    public Stutest(String n,String s,String b,String e, String m) {
    super(n,s);
    bday=b;
    email=e;
    major=m;
    public static void main(String[] args) {
    try {
    Stutest s = new Stutest("Bode", "10", "28/02/1977", "[email protected]", "Bsc");
    ObjectOutputStream out = new ObjectOutputStream(System.out);
    out.writeObject( s );
    catch(IOException io)
    { System.out.println("IO Exception caught: " + io);
    }

    Hi,
    If all you want to see on out is the content of your objects, recursively overwrite the public String toString() method to output the properties of your objects.
    ;)

  • Writing and  Reading serialized Objects

    [code=java]
    /*hey guys i'm new to java and i have been given an exercise to make a cd collection, write it into a file and read the data back to the program.
    the program is suppose to show you a menu to select from where you can add, delete, view sort, CD's when you add a CD it must be written to a file as an Object and when you want to view CDs or search for a CD the program must read the CD objects from the file they have been written to and must return a cd nam, artist and release date. the code looks like it is writing the Cd to a file but when i try to read (view or search for a cd from the file it gives an error null). so i think i'm note reading the right way.
    thank you for helping .
    import java.io.Serializable;
    public class cd implements Serializable {
         //creating attributes
              private String cdname = null;
              private double price = 0.0;
              private String artist =null;
              private int ratings =0;
              private String genre=null;
              private String releaseDate =null;
         // creating an Empty constructor
              public cd(){
              public cd (String cdname,double price, int ratings, String genre, String artist, String releaseDate){
              this.cdname=cdname;
              this.price=price;
              this.artist=artist;
              this.ratings=ratings;
              this.genre=genre;
              this.releaseDate=releaseDate;
              public String getGenre(){
                   return genre;
              public void setGenre(String genre){
                   this.genre =genre;
              public String getArtist(){
                   return artist;
              public void setArtist(String artist){
                   this.artist=artist;
              public String getName(){
              return cdname;
              public void setName(String cdname){
              this.cdname = cdname;
              public Double getPrice(){
              return price;
              public void setPrice(double price){
              this.price = price;
              public String getReleaseDate(){
              return releaseDate;
              public void setReleaseDate(String releaseDate){
              this.releaseDate = releaseDate;
              public int getRatings(){
              return ratings;
              public void setRatings( int ratings){
              this.ratings = ratings;
    import java.util.*;
    public class hipHopCollection {
    ArrayList<cd> list = new ArrayList <cd> ();
    EasyIn ei = new EasyIn();
         private cd invoke;
         private int b;
         public void load()
              System.out.println(" You Entered " + b + " To Add A CD ");
              invoke = new cd();
              System.out.println("Please Enter A CD Name ");     
              invoke.setName(ei.readString());
              System.out.println("Please Enter A CD Price");
              invoke.setPrice(ei.readDouble());
              System.out.println("Please Give Ratings For The CD");
              invoke.setRatings(ei.readInt());
              System.out.println("Please Enter A CD release date ");
              invoke.setReleaseDate(ei.readString());
              System.out.println("Please Enter artist Name ");
              invoke.setArtist(ei.readString());
              System.out.println("Please Enter A CD Genre ");
              invoke.setGenre(ei.readString());
              list.add(invoke); // trying to add cd information to invoke.
         }// end of load
    // The following method should return the Object variable invoke that holds the cd INFO
         public Object getInvoke()
         return invoke;
         public int getB()
         return b;
         public void setB()
         b=ei.readInt();
         public void menu(){
              System.out.println("......................................................... ");
              System.out.println("Hi There Please Enter A Number For Your Choice");
              System.out.println(" Pess >>");
              System.out.println("1 >> Add A CD");
              System.out.println("2 >> View List Of CD's");
              System.out.println("3 >> Sort CD's By Price");
              System.out.println("4 >> Search CD By Name");
              System.out.println("5 >> Remove CD(s) By Name");
              System.out.println("0 >> Exit");
              System.out.println(".........................................................");
              System.out.print("Please Enter Chioce >> ");     
         }// end of menu
         public void GoodBye()
              System.out.println(" You Entered " + b + " To exit Good_bye" );
              System.exit(0);
         }//end of GoodBye
         public void PriceSort()
              System.out.println(" You Entered " + b + " To Sort CD(s) By price ");
              Collections.sort(list, new SortByPrice());
              for(cd s : list)
              System.out.println(s.getName() + ": " + s.getPrice());
         }// end of PriceSort
         public void NameSearch()
                   System.out.println(" You Entered " + b + " To Search CD(s) By Name ");
                   System.out.println("Please Enter The Name Of The CD You Are Searching For " );
                   String search = ei.readString();
                   for(int i=0; i<list.size();i++){
                   if(search.equalsIgnoreCase(list.get(i).getName() )){
                   System.out.println(list.get(i).getName() + " " + list.get(i).getPrice() + " " + list.get(i).getRatings() + " " + list.get(i).getGenre() );
    }//end of NameSearch
         public void ViewList()
                   System.out.println(" You Entered " + b + " To view CD(s) By Name ");
                   for(int i=0; i<list.size();i++)
                   System.out.println(list.get(i).getName() + " " + list.get(i).getPrice() + " " + list.get(i).getRatings() + " " + list.get(i).getGenre() );
         }// end of ViewList
         public void DeleteCd()
                   System.out.println(" You Entered " + b + " To Delete CD(s) By Name ");
                   System.out.println("Please Enter The Name Of The CD You Want to Delete ");
                   String search = ei.readString();
                   for(int i=0; i<list.size();i++)
                   if(search.equalsIgnoreCase(list.get(i).getName() ))
                   System.out.println(list.get(i).getName());
                   list.remove(i);
         }// end of DeleteCD
         public static void main(String[] args) {
         //creating an Instance of EasyIn by object ei. Easy in is a Scanner class for reading
              EasyIn ei = new EasyIn();
              ArrayList<cd> list = new ArrayList <cd> (); // creating an array cd list
              hipHopCollection call = new hipHopCollection();
              ReadWrite rw = new ReadWrite();
                   while (true){
                   call.menu();
                   call.setB();
                   //b = ei.readInt();
                   if(call.getB()==0)
                        call.GoodBye();
                   if(call.getB()==1)
                        call.load();
                        rw.doWriting();// trying to write the cd object to a file
                   if(call.getB()==2)
                   rw.doReading();// trying to read the cd object from a file
                   //call.ViewList();
                   if(call.getB()==3)
                   call.PriceSort();
                   if(call.getB()==4)
                        call.NameSearch();
                   if(call.getB()==5)
                        call.DeleteCd();
         }// end of while
    }// end of main
    }// end of class
    // importing all the packages that we will use
    import java.io.ObjectInputStream;
    import java.io.FileInputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.io.Serializable;
    public class ReadWrite {
    // these are all the attributes
         private String FileName ="CdCollections.dat";     
         private OutputStream output;
         private ObjectOutputStream oos;
         private FileOutputStream fos;
         private File file;
         private FileInputStream fis;
         private ObjectInputStream ois;
         //creating an empty constructor
         public ReadWrite()
         // we could initialise all the attributes inside this empty constructor
         //creating a constructor with arguments of a file name.
         public ReadWrite(File file)
              this.file=file;
              try
                   //Use a FileOutputStream to send data to a file called CdCollections.dat
                   fos = new FileOutputStream(file,true);
                   Use an ObjectOutputStream to send object data to the
                   FileOutputStream for writing to disk.
                   oos = new ObjectOutputStream (fos);
                   fis=new FileInputStream(file);
                   ois = new ObjectInputStream(fis);
              catch(FileNotFoundException e)
                   System.out.println("File Not Found");
              catch(IOException a)
                   System.out.println(a.getMessage());
                   System.out.println("Please check file permissions of if file is not corrupt");
         }// end of the second constructor
         //the following lines of code will be the accessors and mutators
         * @return the output
         public OutputStream getOutput() {
              return output;
         * @param output the output to set
         public void setOutput(OutputStream output) {
              this.output = output;
         * @return the objStream
         public ObjectOutputStream getOos() {
              return oos;
         * @param objStream the objStream to set
         public void setObjStream(ObjectOutputStream objStream) {
              this.oos = oos;
         public File getFile() {
              return file;
         public void setFile(File file) {
              this.file = file;
         public FileInputStream getFis() {
              return fis;
         public void setFis(FileInputStream fis) {
              this.fis = fis;
         public ObjectInputStream getOis() {
              return ois;
         public void setOis(ObjectInputStream ois) {
              this.ois = ois;
         // the following lines of code will be the methods for reading and writing
    the following method doWriting will write data from the hipHopCollections source code.
    that will be all the cd information.
    Pass our object to the ObjectOutputStream's
    writeObject() method to cause it to be written out
    to disk.
    obj_out.writeObject (myObject);
         public void doWriting()
              hipHopCollection call = new hipHopCollection();
    //creating an Object variable hold that will hold cd data from hipHopCollections invoke
              Object hold = call.getInvoke();// THI COULD BE THE PART WHERE I MADE A MISTAKE
              ReadWrite stream = new ReadWrite (new File(FileName));
              try
              Pass our object to the ObjectOutputStream's
              writeObject() method to cause it to be written out to disk.
              stream.getOos().writeObject(hold);
                   stream.getOos().writeObject(hold);
                   stream.getOos().close();
                   System.out.println("Done writing Object");
              catch (IOException e)
                   System.out.println(e.getMessage());
                   System.out.println("Program Failed To Write To The File");     
              finally
                   System.out.println("The program Has come To An End GoodBye");
         }// end of method DoWriting
    The following method is for reading data from the file written by the above method named
    DoWriting
    // PLEASE NOT THIS IS THE METHOD THAT GIVES ME NULL EXCEPTION
         public void doReading()
         ReadWrite read = new ReadWrite(new File(FileName));
              try{
                   //System.out.println("I AM NOW INSIDE THE TRY TO READ");
                   Object obj = read.getOis().readObject();
                   System.out.println("tried reading the object");
                   cd c = (cd)obj; // trying to cast the object back to cd type
                   System.out.println("I have typed cast the Object");               
                   System.out.println(c.getName());
                   System.out.println(c.getGenre());
                   System.out.println(c.getArtist());
                   System.out.println(c.getPrice());
                   System.out.println(c.getRatings());
                   System.out.println(c.getReleaseDate());
                   read.getOis().close();
              catch(ClassNotFoundException e)
              System.out.println(e.getMessage());
              System.out.println("THE CLASS COULD NOT BE FOUND");
              catch(IOException e)
              System.out.println(e.getMessage());// null
              System.out.println("WE COULD NOT READ THE DATA INSIDE THE FILE");
         }//end of method doReading
    }// end of class ReadWrite

    Cross posted
    http://www.java-forums.org/new-java/59965-writing-reading-serialized-java-object.html
    Moderator advice: Please read the announcement(s) at the top of the forum listings and the FAQ linked from every page. They are there for a purpose.
    Then edit your post and format the code correctly.
    db

  • Reading an object from a binary file

    i am writing objects into my binary file using printwriter class. i am able to write objects into the file but i am having problems reading the object from the file. is there any other way of going about it. i tried using the objectoutputstream and object input stream class. but i am getting run time errors coz of something to do with serialization
    i am storing records as a object into a binary file so that it is easy to seek my records

    Of course you have trouble reading objects after you wrote them with a PrintWriter.
    You should rather have fixed the Serialization errors: only objkects that implement Serializable correctly can be serialized.

  • Calling Stored Procedure(takes varray of objects as input) from jdeveloper

    How can i call Stored Procedure which takes varray of objects as input from jdeveloper
    My object is :
    TYPE Entry IS Object (
    Name VARCHAR2(1024),
    Value VARCHAR2(1024)
    & the varray is :
    TYPE EntryArr IS varray(10) OF Entry ;
    & the procedure is :
    PROCEDURE myProc( myEntryArr IN EntryArr )
    AS
    s varchar2(1024);
    BEGIN
    for i in 1.. myEntryArr .COUNT loop
    if myEntryArr(i).Name = 'Name1' then
    s := myEntryArr(i).Value
    end loop;
    end;

    hi 429071
    Maybe you can find some useful information in:
    "Oracle Database Java Developer's Guide"
    http://download-west.oracle.com/docs/cd/B14117_01/java.101/b12021.pdf
    see "6 Publishing Java Classes With Call Specs" > "Writing Object Type Call Specs"
    success
    Jan Vervecken

Maybe you are looking for

  • Can I get my Game Achievements back on a new device? But not from a backup.

    I am wanting to get the new iPod Touch 5th Gen, But i'm wanting to start fresh but I want to hang onto my Game Achievements. (Game Stats and Highscores) So is it possible for me to start fresh and log into Game Center and get all my Highscores and St

  • Performance question on small XML content but with large volume

    Hi all, I am new to Berkeley XML DB. I have the following simple XML content: <s:scxml xmlns:s="http://www.w3.org/2005/07/scxml"> <s:state id="a"/> <s:state id="b"/> <s:state id="c"/> </s:scxml> about 1.5K bytes each but the total number of such cont

  • ABAP Debugger - modify values

    Hi, In the new ABAP debugger when you double click on a variable there is a little pencil icon to the change the content of the variable. If the variable has more than 255 characters or if you chnage the variable to a long text of more than 255 then

  • Connecting Dell laptop to Vizio TV

    I want to connect my Dell vostro 1500 laptop to my Vizio tv e370vl  . I want to be able to stream netflix from online. Is there some type of wires or usb i need to connect to the two? Solved! Go to Solution.

  • How can I select which photo is used on the cover of an i-Photo book?

    I have created a photobook, and can do most things I need, but the photo selected by Autoflow for the cover is not the one I want. Can anyone tell me how to change it?