,
Share with your friends 

writing objects into file

7 ratings Views 81 
Author: sachin_22461307 (sachin K)  View Profile |  View other solutions by this author

Question / Problem


How to write serialized objects(single,multiple dimensions) into file ?

Solution

import java.io.*;
/**
    *Class used to store objects into file
    *This is a sample program to store String array into file
    * like this we can store any thing which are serialized object
    * Non-serialize objects (like status of checkbox) are not able to store
*/
public class WriteObjToFile{
     public static void main(String argv[]){
          /** String path for linux user for windows use C:\filePath*/
          String strPath = "/home/username/desktop/";
          String strFileName = "somename.info";
          String []strArrNames = {"Sachin","Deepak","Sandeep"};
          System.out.println("Data writing has start");
          File f = new File(strPath+strFileName);
          FileOutputStream fileop = new FileOutputStream(f);
          fileop.writeObject(strArrNames);
          fileop.close();//Dont forget to close the file
          fileop.flush();//dont forget to flush
          System.out.println("The data has been written");
     }
}

Applies to

Core Java

Rank It

Login to rank it

Report


Advertisement