[go: up one dir, main page]

0% found this document useful (0 votes)
118 views13 pages

File Handling in Java

The document describes two Java programs that write user information to a file and read it back. The first program uses a predefined array of activities, while the second takes user input for all information including activities. Both programs use serialization to write a MyLockdownActivities object and an activities array to a file, then deserialize and print the read data.

Uploaded by

Ankur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views13 pages

File Handling in Java

The document describes two Java programs that write user information to a file and read it back. The first program uses a predefined array of activities, while the second takes user input for all information including activities. Both programs use serialization to write a MyLockdownActivities object and an activities array to a file, then deserialize and print the read data.

Uploaded by

Ankur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

JAVA PROGRAMMING

DIGITAL ASSIGNMENT 1

NAME – ANKUR KUNDU


REG.NO. – 17BIS0020
COURSE CODE – CSE1007
FACULTY – Prof. JANANI T
SLOT – G2+TG2
Question –
Write your information [as string as well as
MyLockdownActivities_Regno Class’ object] like regno, name,
cgpa, phno, lockdown_actvities array into a file and read from
the file and print in the console. Use Serialization and
Deserialization in the program.

Answer: I have made two different types of programs for this


question. First, I have used a pre-defined string array where
my activities are already mentioned during the String Array
declaration. And for the second one, I have taken all inputs
from the user, the lockdown activities as well, and then
created a function inside MyLockdownActivities_17BIS0020
class, which converts the String array into one single String and
the delimiter, i.e., “,” is replaced by “”, i.e., blank space and
converted into a String and this String is returned by the said
function.

INPUT -
CODE 1 -
MyLockdownActivities_17BIS0020.java
import java.io.Serializable;

import java.util.ArrayList;

public class MyLockdownActivities_17BIS0020 implements Serializable {

public String regno;


public String name;
public double cgpa ;
public String Phno;

Main.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class Main {


public static void main(String []args)
{
MyLockdownActivities_17BIS0020 obj = new
MyLockdownActivities_17BIS0020();
obj.name = "Ankur Kundu";
obj.regno = "17BIS0020";
obj.Phno = "7980012993";
obj.cgpa = 8.97;
ArrayList<String> ActivityList = new ArrayList<String>();
ActivityList.add("Reading Books");
ActivityList.add("BInge watching on OTT Platform");
ActivityList.add("Spending time with Mickey");

String fileName = "data.txt";


try {
ObjectOutputStream os = new ObjectOutputStream(new
FileOutputStream(fileName));
os.writeObject(obj);
os.writeObject(ActivityList);
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Done Writing");

try {
ObjectInputStream is = new ObjectInputStream(new
FileInputStream(fileName));
MyLockdownActivities_17BIS0020 p =
(MyLockdownActivities_17BIS0020) is.readObject();
System.out.println("Read name = " + p.name + '\n' + "Read
regno = " + p.regno);
System.out.println("Read cgpa = " + p.cgpa + '\n' + "Read Phno
= " + p.Phno);
System.out.println("Activities are : " + ActivityList);
System.out.println();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

CONSOLE OUTPUT -

FILE OUTPUT (data.txt) -


INPUT –
CODE 2 –
DigitalAssignment1.java
/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package javaapplication2;

/**

* @author Ankiur

*/

import java.io.Serializable;

import java.io.*;

import java.util.*;

class MyLockdownActivities_17BIS0020 implements Serializable {

public String name;

public String regno;

public double cgpa ;

public String Phno;

public String[] activities;

MyLockdownActivities_17BIS0020()

Scanner sc = new Scanner(System.in);

System.out.println("Enter your name: ");

name = sc.nextLine();

System.out.println("Enter your Registration Number: ");

regno = sc.next();
System.out.println("Enter your CGPA: ");

cgpa = sc.nextDouble();

System.out.println("Enter your Phone number: ");

Phno = sc.next();

/*public String[] Activities() {

// TODO Auto-generated method stub

Scanner sc = new Scanner(System.in);

System.out.println("How many lockdown activitiesdo you have?: ");

int n = sc.nextInt();

String[] activities = new String[n];

System.out.println("Enter your lockdown activities: ");

for(int i=0; i<n;i++)

activities[i] = sc.next();

return activities;

}*/

public String Activities(String arr[])

//String Activities[] = {"Eating", "Sleeping", "Netflix"};

String str = Arrays.toString(arr);

str = str.substring(1, str.length()-1).replace(",", "\n");

return str;

public class DigitalAssignment1 {

public static void main(String []args)

MyLockdownActivities_17BIS0020 obj = new


MyLockdownActivities_17BIS0020();

/*obj.name = "Ankur Kundu";

obj.regno = "17BIS0020";
obj.Phno = "7980012993";

obj.cgpa = 8.97;*/

Scanner sc = new Scanner(System.in);

System.out.println("How many lockdown activities do you have?: ");

int n = sc.nextInt();

obj.activities = new String[n];

System.out.println("Enter your lockdown activities: ");

for(int i=0; i<n;i++)

sc.nextLine();

obj.activities[i] = sc.nextLine();

obj.Activities(obj.activities);

String fileName = "data.txt";

try {

ObjectOutputStream os = new ObjectOutputStream(new


FileOutputStream(fileName));

os.writeObject(obj);

os.flush();

os.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("Done Writing\n");

try {

ObjectInputStream is = new ObjectInputStream(new


FileInputStream(fileName));

MyLockdownActivities_17BIS0020 p =
(MyLockdownActivities_17BIS0020) is.readObject();
System.out.println("-----Reading from the text file-----");

System.out.println("Name = " + p.name + '\n' + "Registration


Number = " + p.regno);

System.out.println("CGPA = " + p.cgpa + '\n' + "Phone number =


" + p.Phno);

System.out.println("My lockdown activities are: ");

System.out.println(p.Activities(p.activities));

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

FILE OUTPUT (data.txt) –

¬í sr /javaapplication2.MyLockdownActivities_17BIS0020ímúælnS D
cgpaL
Phnot Ljava/lang/String;[
activitiest [Ljava/lang/String;L
nameq ~ L regnoq ~ xp@!ð£×

=qt

7980012993ur [Ljava.lang.String;-ÒVçé{G xp
t Eatingt Sleepingt )Binge watching and doing Coursera coursest Playing with my pet Mickeyt
Ankur Kundut 17BIS0020

CONSOLE OUTPUT –

You might also like