Java Program for File Input/Output, Serialization, and Deserialization

Verified

Added on  2020/09/25

|3
|394
|35
Homework Assignment
AI Summary
This Java assignment comprises two parts. The first part focuses on object serialization and deserialization, where a `MyInfo` class is created and its objects are written to a file using `FileOutputStream` and `ObjectOutputStream`. The program then reads the object from the file using `FileInputStream` and `ObjectInputStream`, demonstrating the process of deserialization. The second part involves file input/output and string manipulation. It reads a file, counts the occurrences of the strings “CSE1007” and “JAVA”, and prints the counts to the console. The program uses `FileInputStream` to read the file and splits the content based on the target strings to count their occurrences. This assignment provides practical examples of file handling and object serialization in Java.
Document Page
NAME:SHRAVAN S
REGISTRATION NUMBER:19BCE2548
1)Write your informaton [as string as well as MyInfo Class object] like regno, name, cgpa and phno
into a fle and read from the fle and print in the console. Use Serializaton and Deserializaton in
the program.
package com.package2;
import java.io.*;
class MyInfo implements Serializable{
String name, registration;
double cgpa;
long phone;
public MyInfo(String Name,String regno,double cgpa,long phno){
this.name =Name;
this.registration =regno;
this.cgpa=cgpa;
this.phone =phno;
}
}
public class Main{
public static void main(String[] args) throws
FileNotFoundException,IOException,ClassNotFoundException{
MyInfo s1=new MyInfo("Shravan","19BCE2548",8.21,769584477);
FileOutputStream fout=new FileOutputStream("C:\\Users\\SID\\Documents\\
MyInfo.txt");
FileInputStream fin=new FileInputStream("C:\\Users\\SID\\Documents\\
MyInfo.txt");
ObjectOutputStream out=new ObjectOutputStream(fout);
out.writeObject(s1);
out.flush();
System.out.println("Serialization successful!");
System.out.println();
ObjectInputStream in=new ObjectInputStream(fin);
MyInfo s2=(MyInfo)in.readObject();
System.out.println(s2.registration +" "+s2.name +" "+s2.cgpa+" "+s2.phone);
System.out.println("Deserialization successful!");
}
}
OUTPUT:
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
2)Traverse a fle and count the words “CSE1007” and “JAVA”, print the counts in the console
package com.package2;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
Document Page
int JavaCount=0,TxtCount=0;
File file = new File("///C:/Users/SID/Documents/mast.txt");
FileInputStream Sid = new FileInputStream(file);
byte[] bytesArray = new byte[(int)file.length()];
Sid.read(bytesArray);
String sid = new String(bytesArray);
String [] num = sid.split("CSE1007");
String [] num1=sid.split("JAVA");
System.out.println("Number of JAVA characters " +num1.length);
System.out.println("Number of CSE1007 characters " +num.length);
}
}
OUTPUT:
chevron_up_icon
1 out of 3
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]