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,
- [Fix] java.time.zone.ZoneRulesException: Unknown time-zone ID
- Parse XML file in Java using DOM Parser
- Java equals method - Tutorial
- [Program] How to read three different values using Scanner in Java
- Java: The value of the local variable string is not used
- Display Output in Java Console as a Table
- How to detect Operating System using Java code
- Java 8 Streams map() with examples
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error
- Add newline character Java code example (\r \n \r\n)
- List of Java Major Minor Version Numbers
- IntelliJ Keyboard Shortcut to remove unused imports [Java]
- Java - Check if array contains the value
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- XmlRpcException ConnectException connection refused error
- Create a Zip file using Java Code programmatically
- List of jar files for Jax-ws (SOAP) based Java Web Services
- How to fix Java HTTP java.net.UnknownHostException
- List of jars required for Struts2 project
- [fix] java: incompatible types: double cannot be converted to java.lang.Integer Generics
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- Get the current timestamp in Java
- java: unclosed string literal [Error]
More Posts:
- How to get an embed code from Vimeo? - HowTos
- How to install Python 3.9 using brew on Mac - Python
- 🔥 [BTS ARMY] #ExaBLINK, #ExaARMY hashtags trending worldwide - BTS
- fill_parent vs match_parent vs wrap_content - Android
- JavaScript date in yyyy-MM-dd format - JavaScript
- How to make zsh as the default shell on Ubuntu - zsh
- Locate Notepad++ unsaved files backup location - NotepadPlusPlus
- MySQL #6 - Error on delete of './my-database/db.opt' (Errcode: 13 - Permission denied) - MySQL