Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Experiment No- 9

// Name
Roll No.-159
//Div:B

Title: Implementation of reading student data and store it into file.


Problem Statement:
Take Student information such as name, age, weight, height, city, phone from
user and store it in the file using DataOutputStream and FileOutputStream and
Retrive data using DataInputStream and FileInputStream and display the result.
Use serialization concept and Bytestream classes.

Program :
import java.io.*;
import java.util.Scanner;
class StudInfo{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
try{
//FileOutputStream f1=new FileOutputStream("Student.txt");
//DataOutputStream d1 = new DataOutputStream(f1);
DataOutputStream d1 = new DataOutputStream(new
FileOutputStream("Student.txt"));
String name,age,weight,height,city,phone;
System.out.println("Enter your name: ");
name=sc.next();
System.out.println("Enter Your age:");
age=sc.next();

System.out.println("Enter Your weight:");


weight=sc.next();

System.out.println("Enter Your height:");


height=sc.next();

System.out.println("Enter Your city:");


city=sc.next();

System.out.println("Enter Your phone:");


phone=sc.next();

d1.writeBytes(System.lineSeparator()+name);
d1.writeBytes(System.lineSeparator()+age);
d1.writeBytes(System.lineSeparator()+weight);
d1.writeBytes(System.lineSeparator()+height);
d1.writeBytes(System.lineSeparator()+city);
d1.writeBytes(System.lineSeparator()+phone);
d1.close();

//FileInputStream f2=new FileInputStream("Student.txt");


//DataInputStream d2 = new DataInputStream(f2);

DataInputStream d2 = new DataInputStream(new


FileInputStream("Student.txt"));
int s;
while((s=d2.read())!=-1)
{
System.out.print((char)s);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Output:

You might also like