LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-06-2004, 03:25 PM   #1
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Rep: Reputation: 31
how to transform an object into an array of bytes on Java!


hello

I have an Object a and I wanna be able to save it's state on a random access file. I've learned that in order to write on a file I ne ed to transform it into a byte array. But how can I do it?

is there something like a.tobyteArray() ???

once that I've saved it, how can I retreive it for future use?


Thanks thanks thanks a lot!
 
Old 02-06-2004, 03:40 PM   #2
ter_roshak
Member
 
Registered: May 2001
Location: Everett, WA
Distribution: Gentoo, RedHat
Posts: 102

Rep: Reputation: 15
Have you looked into the 'serializeable' interface? It might do what you want, it allows you to save objects and restore them later.

-Josh
 
Old 02-15-2004, 12:26 PM   #3
german
Member
 
Registered: Jul 2003
Location: Toronto, Canada
Distribution: Debian etch, Gentoo
Posts: 312

Rep: Reputation: 30
This code does what you want... you should read the sun docs on the ObjectOutputStream object here: http://java.sun.com/j2se/1.4.1/docs/...putStream.html

Code:
import java.util.*;
import java.io.*;
                                                                                
public class ObjOut {
        public static void main(String[] args) throws Exception {
                                                                                
                // create some object
                ArrayList list = new ArrayList();
                list.add("One");
                list.add("Two");
                list.add("Three");
                                                                                
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                ObjectOutputStream out = new ObjectOutputStream(bos);
                out.writeObject(list);
                                                                                
                RandomAccessFile raf = new RandomAccessFile(
                        new File("./list"), "rw"
                );
                raf.write(bos.toByteArray());
                raf.close();
        }
}
Of course the code could be made more efficient by constructing the ObjectOutputStream with a FileOutputStream, instead of buffering the whole object in memory then writing it through the RandomAccessFile, but that's what you asked for.

HTH

B.
 
Old 02-15-2004, 07:28 PM   #4
poeta_boy
Member
 
Registered: Oct 2003
Location: Mexico
Distribution: Ubuntu
Posts: 223

Original Poster
Rep: Reputation: 31
thanks a lot! that's exactly what I was looking for. Thanks!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Object Reference problem in Java servlets Hockeyfan Programming 1 06-16-2005 01:36 AM
java works but only part with php to transform xml richard22 Programming 4 03-20-2005 10:42 PM
Java Applets Konqueror Object Tag lel800 Programming 0 12-30-2004 02:40 PM
java object with a String and an Image exodist Programming 1 05-15-2004 12:36 AM
Bad vibes in Java - bytes turning negative MadCactus Programming 3 08-29-2003 06:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration