How do i copy (save) an object to a file

hello
i am a programmer (but do not have so much experience in java more c, c++ and matlab)
i have a question
how do i copy an object to a file?
i have Object obj and i want to have a class with two methods save ( save it to a file') and load (open this saved file)
class SaveFile {
Object obj
// other fields perhaps if it is necessary
public save (Object obj) {
//code if which i don't know how to write and i need help from you
public load (object obj) {
//code if which i don't know how to write and i need help from you
i believe it is a standard think whith a standard solution so i hope people can show the complete code (or pseudocode) because i am not so good in java's io.package

Here's a small example that uses no expert features:import java.io.*;
public class Test {
    public static void main (String[] parameters) throws Exception {
        write (new TestObject ("Test"), "test.obj");
        System.out.println (((TestObject) read ("test.obj")).getName ());
    private static class TestObject implements Serializable {
        private String name;
        public TestObject (String name) {
            this.name = name;
        public String getName () {
            return name;
    private static void write (Serializable object, String filename) throws IOException {
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream (new FileOutputStream (filename));
            out.writeObject (object);
            out.flush ();
        } finally {
            if (out != null) {
                try {
                    out.close ();
                } catch (IOException exception) {}
    private static Object read (String filename) throws ClassNotFoundException, IOException {
        ObjectInputStream in = null;
        try {
            in = new ObjectInputStream (new FileInputStream (filename));
            return in.readObject ();
        } finally {
            if (in != null) {
                try {
                    in.close ();
                } catch (IOException exception) {}
}

Similar Messages

  • How can i copy,paste an Object ?

    How can i copy,paste an Object which i define by myself?

    implements Cloneable and define a clone method, for example
    public class ObjectA  implements Cloneable {
       // Data member
       String data;
       public ObjectA() {
       public void setData(String data) {
          this.data = data;
       public Object clone() {
           ObjectA tempObject = new ObjectA();
           tempObject.setData(this.data);
           return tempObject;
    }

  • Save different Objects in one file by two different streams

    Hi all,
    I have a issue to read two Objects from one file if I write those in two different streams.
    I open the ObjectOutputStream and save an Object to the file. Then I close this stream and file. Then I open the new ObjectOutputStream and save the second Object in the same file. It works Ok. Then I open the ObjectInputStream and try to read those two objects and get java.io.StreamCorruptedException.
    This exception happes only when app tryes to read the second Object.
    this is the code example:
    //Write part
    for (int i=0;i<2;i++)
    File file = new File("test.swp");
    FileOutputStream fileOut = new FileOutputStream(file,true);
    ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
    JobObject jobObj = new JobObject();
    jobObj.setID(i);
    objOut.writeObject(jobObj);
    objOut.flush();
    objOut.close();
    fileOut.close();
    //Read part
    FileInputStream fileOut = new FileInputStream("test.swp");
    ObjectInputStream objOut = new ObjectInputStream(fileOut);
    for (int i=0;i<2;i++)
    JobObject jobObj = (JobObject)objOut.readObject();
    objOut.close();
    fileOut.close();
    Thank for any help.

    Maybe try closing the ObjectInputStream (and re-creating it as needed) within your for loop?
    - Saish
    "My karma ran over your dogma." - Anon

  • How can I copy part of a larger PDF file and create a smaller one?

    How can I copy part of a larger PDF file and create a smaller one?

    Hi,
    Copy the content of PDF file in Adobe Reader and paset it to MS Word then create PDF file from the Word file.
    This may not work for all PDF files as some PDF files are not created correctly or some content cannot be copied.
    Acrobat XI Pro has features to extract/delete/crop pages in PDF files and you can download it from www.adobe.com(Trial version- 30 days free). Or you can upgrade your subscription to Acrobat Plus and see more information at https://www.acrobat.com/acrobatplus/en/home.html
    Hisami

  • How do I copy a home made HD video file (MP4 codec) of 10 GB from an external hard disk to my Mac Book Pro? Thanks

    How do I copy a home made HD video file (MP4 codec) of >10 GB from an external hard disk to my Mac Book Pro? Thanks

    I will never know if the problem was one of transfer or format compatibility.  The movie would not copy from one external hard diask to another, both attached to my MAC. But when the movie (which was in mpg by the way) was downloaded directly (without any other intermediary) into my external disk from the computer (a PC) where the movie was originated, the file did copy. And when I attached that external hard disk to my MAC, i could see it  using the MPEC streamclip app.
    Thank you anyway!

  • How do I copy text and objects from pages to and paste into Email, objects don't show in Email

    Anyone know how I copy text and objects from pages and paste into Email, objects don't show in Email, thanks

    You can't expect Mail to support all the objects of Pages which can include anything up to charts generated from spreadsheet tables.
    Even if Mail was a superset of Pages, what would the recipients who don't have either Mail or Pages, make of it?
    Peter

  • How can I copy the layer from one .PSD file to another .PSD file?

    Hi,
    Actually it is shape and mask on one layer which I want to use it on my Photoshop CS4 file. If I select all the layer and copy it to my file it just copies the shape but not the mask. How can I copy MASK to my file?
    Please do hlep.
    Actually I am trying to copy the following layer.

    I believe the fastest way would be to duplicate the layer and send it to the other document. Open both images. Click the document tab of the document with the shape layer. With the shape layer selected in the layers palette, right click where it says the shape layer name. Select "duplicate layer" from the context menu. In the dialog that comes up, select the document you want to copy that layer to in the destination box then press ok.
    Alternately, you can open both images then pull the document frame tabs down to open both images in your workspace. Grab the shape layer in the document's layers palette by left clicking it...keep the left button pressed (hand icon should be a grab icon). While left button is still pressed, drag the shape layer over the receiving document. To make the shape layer register in the same location, press the shift key and keep it pressed. Release left mouse button while the layer is over the receiving document. Release shift key after you have released the left mouse button.
    This tutorial has a good photo reference for what I mean when I say pull the image tabs down so both documents appear in the same workspace...also shows drag/drop:
    http://www.photoshopcstutorials.co.uk/html/cs4_workspace___palette_notes.html

  • How do I copy editted clips from one project file to another?

    I am working on a project. At one point, before I finished I "saved as" with another name to give me a backup. After working on it for a while I found that a section of the video had gotten out of place (a lot of clips and layers). I would like to copy this portion from the other project file I had saved. I can't figure it out. Both projects have the same clips in the project bins. I am using Premiere 6.5, with a Pinnacle DV500 DVD capture card. My computer is a duel P3 800, 512RAM, 40Gig Sys. drive and 480 Gig RAID 0. Please Help!

    Throw away everything you don't want to copy, save as a new project, import the new project into the other one.

  • How do you put Save as back in the File menu?

    I know under File the "save as" is gone. I also know that if you push the option key it will show up under the File menu until you take your finger off the option key. What I want to know is how can you get the "save as" back under the file menu without pushing another key. I had someone tell me how to do it in Mavericks but I can't seem to remember how he told me to do it. I appreciate any help here!
    I'm on a 13" MacBook Pro with Retina and running Yosemite.
    Thanks

    You can try this or do a Google search to find alternatives.

  • How do i copy and paste signature to pdf file

    how do i copy and paste my signature to a pdf. file

    You don't.  You include your signature in the file that PDF was created from.

  • How can we create a ABAP object into .ptr file

    HI,
          How can we create a ABAP object into .ptr/.car/.sar format so that we can give it to a remote client and which can be imported into their R/3 system to get the functionality of this object.

    Hello Ramesh
    All you need is SAPLINK. Have a look at the following links:
    <a href="/people/ed.herrmann/blog/2005/11/14/the-enterprise-and-the-bazaar Enterprise and the Bazaar</a>
    <a href="https://sourceforge.net/projects/saplink/">SAPLINK</a>
    Regards
      Uwe

  • How do I copy an art object from one document to another using illustrator API(C  )

    Hi, I'm trying to copy text and graphic elements from one document into another, does anybody has an idea how to do it using Illustrator SDK(C++)?
    Thanks in advance.

    If you call AIArtSuite::ReorderArt() giving the layer group handle from the target document and using art from the source document, it will move art between documents. That said, there are all sorts of caveats that come along with that -- some things move over automatically (graphic styles, symbols) while others cause problems. E.g. copying art that contains a swatch can cause a crash when you shut down the source document because it may keep the style reference to the swatch in the source document -- which goes away when the document is closed. Gradients in particular are a problem I believe. Straight up colours are (I think) fine. I've had to write a lot of workarounds for various headaches caused by moving art between documents.
    Bottom line: its very doable, but there are a lot of edge cases. This was clearly not an intended use of the SDK, though it is possible.

  • How do you copy paste a thumbnail from one file to another in Mavericks

    I used to be able to do a copy/paste and duplicate in the thumbnails...how do I do now in Maverick?

    In the Pages and Numbers applications, the thumbnail view of my document no longer allows me to re-arrange, duplicate or otherwise edit the thumbnails. The fact that I can't right click on the thumbnails to manage the pages are one thing, but the fact that I can't even drag and drop the order of pages is unbelievable. Suggestions?

  • How can I copy a table from a web file and paste it into a word document on a pc?

    I am having diffculty getting a pdf file from a web document pasted into my word document.  I do no get the option to copy or save as a table.  I can copy and paste the text but I lose the table and the format and just get the text.  Anybody know what I can do to solve this?

    Try holding down the alt key as you mark the text to be copied. You can then copy columns to table text.

  • How do I copy text from an uploaded pdf file?

    I have a document and want to copy the text of one page into a new document.  How do I proceed to do that?  I am using the Adobe cloud product.
    Thanks in advance.

    Just some observations.
    PDF page content that is an image (and music notes may very well be that in a given PDF) then all that can be exported is the image.
    Only an image editor can "edit" such eh.
    As to textual content - once in TXT, RTF, DOC or DOCX yes, expect to have to do "cleanup"
    Of course there's the old school alternative that is always available.
    Paper copy beside you as you transcribe to a fresh word processing file.
    Be well...

Maybe you are looking for

  • Animated GIF Doesn't Work At All

    I am a Web developer. My client has provided us with an animated GIF image for their landing page. This GIF runs fine on every browser except Safari. On Safari, the GIF never animates; it just stays on the opening frame and looks stupid. This happens

  • How can I sync my iMac & iPod Touch calendar & contacts with new iTunes?

    I have just installed the latest iTunes to my iMac. My iPod Touch has iOS 6.0.1. My iMac has OS 10.6.8. I sync by Wi Fi via iCloud. My calendar and contacts are not syncing by Wi Fi between the two, although my mail is. I What should I do?

  • Original iPad mini had water damage, then sat around for 8 months.

    I have an iPad mini with water damage that has been sitting around for 8 months. I woke up in the middle of the night and spilled water on it. It was plugged in and in an Otterbox. I unplugged it, took it out of the case, and dried it off. I then put

  • Creating new PCR Scenario in ABAP Workbench

    Hi everyone, I'm trying a lot to create a new scenario for the PCR functionality of Manager Self Service but I'm having some problems. I enter in the transaction QISRSCENARIO and create a new adobe form from scratch. I created some characteristic and

  • Photoshop Elements 10 will not open

    Installing the program went fine, but when I click on the icon on the desktop nothing seems to happen. A background process is started though (the Welcome screen, I think), which I have to go to Task Manager to close. I have never received any error