java how to serialize a file into a local server

Solutions on MaxInterview for java how to serialize a file into a local server by the best coders in the world

showing results for - "java how to serialize a file into a local server"
Stefania
06 Mar 2020
1package com.techbeamers.serialization;
2
3import java.io.FileInputStream;
4import java.io.FileOutputStream;
5import java.io.IOException;
6import java.io.ObjectInputStream;
7import java.io.ObjectOutputStream;
8
9public class SerializationLib {
10
11    // Do serialize the Java object and save it to a file
12    public static void doSerialize(Object obj, String outputFile)
13            throws IOException {
14        FileOutputStream fileTowrite = new FileOutputStream(outputFile);
15        ObjectOutputStream objTowrite = new ObjectOutputStream(fileTowrite);
16        objTowrite.writeObject(obj);
17 
18        fileTowrite.close();
19    }
20
21    // Do deserialize the Java object from a given file
22    public static Object doDeserialize(String inputFile) throws IOException,
23            ClassNotFoundException {
24        FileInputStream fileToread = new FileInputStream(inputFile);
25        ObjectInputStream objToread = new ObjectInputStream(fileToread);
26        Object obj = objToread.readObject();
27        objToread.close();
28        return obj;
29    }
30 }
31
similar questions