import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class JavaSerializationExample {
public static void main(String[] args) {
String programmingLanguages[] = new String[5];
programmingLanguages[0] = "Java";
programmingLanguages[1] = "JavaScript";
programmingLanguages[2] = "Php";
programmingLanguages[3] = "DotNet";
programmingLanguages[4] = "C++";
try {
System.out.println("Serializing object ProgrammingLanguages ");
FileOutputStream fileOutputStream = new FileOutputStream("languages.ser");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(programmingLanguages);
objectOutputStream.close();
fileOutputStream.close();
System.out.println("Serialized object Stored as ProgrammingLanguages.ser ");
}catch(IOException e) {
e.printStackTrace();
}
}
}
Deserializing a Java Object:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class JavaSerializationExample {
public static void main(String[] args) {
String programmingLanguages[] = new String[5];
try {
System.out.println("Deserializing object ProgrammingLanguages ");
FileInputStream fileInputStream = new FileInputStream("languages.ser");
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
try {
programmingLanguages = (String[]) objectInputStream.readObject();
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
objectInputStream.close();
fileInputStream.close();
for (int i = 0; i < programmingLanguages.length; i++) {
System.out.println(programmingLanguages[i]);
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
Output:
Deserializing object ProgrammingLanguages
Java
JavaScript
Php
DotNet
C++
More Posts related to Java,
- Java XML-RPC 3.1.x based web service example
- List of jars required for Struts2 project
- list of jars required for hibernate 4.x.x
- How to create StackOverflow error in java
- Tutorial Java SOAP WebServices JAS-WS with Eclipse J2EE IDE and Tomcat Server Part 1
- import servlet API to eclipse project (javax.servlet cannot be resolved error)
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- Convert String to int in Java
- JSP Hello World Program Tutorial with Eclipse and Tomcat Server
- SharePoint Open in the client application document opens in browser
- Java: TimeZone List with GMT/UTC Offset
- Struts2 : java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
- Remove Trailing zeros BigDecimal Java
- [Solution] Java Error Code 1603. Java Update did not complete.
- Maven Eclipse (M2e) No archetypes currently available
- error: file not found: HelloWorld.java
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to check if Java main thread is alive
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- Java XML-RPC java.net.BindException: Address already in use
- Eclipse : The type java.lang.CharSequence cannot be resolved. Indirectly referenced from required .class files
- Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end users experience
- Eclipse : A java Runtime Environment (JRE) or Java Development kit (JDK) must be available
- List of jar files for Jax-ws (SOAP) based Java Web Services
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Maven Unsupported major.minor version 51.0
- hibernate.cfg.xml Configuration and Mapping xml Example
- How to verify if java is installed on the computer and get version detail
- Unhandled exception type InterruptedException : Java Threads
- XmlRpcException ConnectException connection refused error
- Error: Unable to access jarfile jarFileName.jar file [Windows]
- BeanDefinitionStoreException IOException parsing XML document from class path resource [spring.xml]
- How to serialize-deserialize an object in java
- List of Java Keywords
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server
- Struts 2 Hello World Example in Eclipse
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- List of Java versions
- Create simple struts2 project using maven commands
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- Error: Can not find the tag library descriptor for
- connection.url property value in hibernate.cfg.xml for mysql
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Setting Java_Home Environment variable on Windows Operating System
More Posts:
- Android : java.lang.IllegalArgumentException File contains a path separator - Android
- Calculate Volume of Cylinder - C-Program
- How to add hint text in bootstrap input text field and text area - Bootstrap
- Android Studio emulator/Device logCat logs not displayed - Android-Studio
- Copy file from one directory to other in Php - PHP
- Calculate Volume of Ellipsoid - C-Program
- How to get UTC (GMT) using javascript - JavaScript
- AVD Pixel_2_API_15 is already running. [Android Studio] - Android-Studio
- SharePoint 2010 August 2015 Update KB3055049 - Duplicate Document ID issue bug fixed - SharePoint
- Get Browser Screen Width and Height dynamically using jquery - jQuery
- Multiple line editing in Notepad++ - NotepadPlusPlus
- Merge-SPlogfile PowerShell - SharePoint Correlation ID error - SharePoint
- SharePoint Server 2016 Preview installation error - This Product Key isn't a valid Microsoft Office 2016 Product Key. Check that you've entered it correctly. - SharePoint
- How to kill tomcat server process using Mac Terminal Command - Tomcat
- Detect Data roaming in Android Application - Android