Co3 Ass

You might also like

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

REGISTER NO : 1612075 NAME: P.

Pooja

National Engineering College,K.R.Nagar, Kovilpatti

(An Autonomous Institution Affiliated to Anna University Chennai)


Department of Computer Science and Engineering

Assessment Questions for CO3

1. Write a console application for reading student name, Reg.no, Dept, Gender, Native
place from the user and write the details into file in binary format use appropriate
reader and writer class? CO3

PROGRAM
using System;
using System.IO;

public class BinStream


{
public BinStream()
{
Writer();
Reader();
}

public static void Main()


{
BinStream bs = new BinStream();
Console.ReadLine();
}

private void Writer()


{
try
{
Console.Out.WriteLine("Preparing to Write ...");

//Open a FileStream on the file "aboutme"


FileStream fout = new FileStream("aboutme.txt", FileMode.OpenOrCreate,
FileAccess.Write, FileShare.ReadWrite);

//Create a BinaryWriter from the FileStream


BinaryWriter bw = new BinaryWriter(fout);

//Create some arbitrary variables


string name = "jano";
int regno = 1612040;
string dept = "cse";
String native="tvl";
REGISTER NO : 1612075 NAME: P.Pooja

char gender = 'M';

//Write the values to file


bw.Write(name);
bw.Write(regno);
bw.Write(dept);
bw.Write(native);
bw.Write(gender);

//Close the file and free resources


bw.Close();
Console.WriteLine("Data Written!");
Console.WriteLine();
}

catch (IOException e)
{
Console.WriteLine("An IO Exception Occurred :" + e);
}
}

private void Reader()


{
try
{
Console.WriteLine("Preparing to Read ...");

//Open a FileStream in Read mode


FileStream fin = new FileStream("aboutme.txt", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);

//Create a BinaryReader from the FileStream


BinaryReader br = new BinaryReader(fin);

//Seek to the start of the file


br.BaseStream.Seek(0, SeekOrigin.Begin);

//Read from the file and store the values to the variables
string name = br.ReadString();
int regno = br.ReadInt32();
string dept = br.ReadString();
String native = br.ReadString();
char gender = br.ReadChar();

//Display the data on the console


Console.WriteLine("Name :" + name);
REGISTER NO : 1612075 NAME: P.Pooja

Console.WriteLine("Regno :" + regno);


Console.WriteLine("Dept :" + dept);
Console.WriteLine("Native :" + native);
Console.WriteLine("Gender M/F:" + gender);

//Close the stream and free the resources


br.Close();
Console.WriteLine("Data Read!");
}

catch (IOException e)
{
Console.WriteLine("An IO Exception Occurred :" + e);
}
}
}
OUTPUT:

2. Write a c# program to create a file and directory and also write the appropriate
methods to get the following details in run time? CO3
REGISTER NO : 1612075 NAME: P.Pooja

 Copy the string


 Open methods
 Create directory
 Append the string
 Delete the string
 Get Attributes
 Get creation time
 Display path, parent and root
Program:
using System;
using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

namespace fileinfoclass1 {

class Program {

static void Main(string[] args) {

// Create Method

FileInfo fi = new FileInfo(@ "D:\MyTestFile7.txt");

FileStream fs = fi.Create();

Console.WriteLine("File has been created");

//CreateText Method

FileInfo fi = new FileInfo(@ "D:\MyTestFilecreatetext1.txt");

StreamWriter str = fi.CreateText();

str.WriteLine("hello");

Console.WriteLine("File has been created with text");

str.Close();

//Delete Method
REGISTER NO : 1612075 NAME: P.Pooja

FileInfo fi = new FileInfo(@ "D:\MyTestFilecreatetext.txt");

fi.Delete();

Console.WriteLine("File has been deleted");

//CreateText Method

string path = @ "d:\MyTestFile7.txt";

string path2 = @ "d:\NewFile.txt";

FileInfo fi1 = new FileInfo(path);

FileInfo fi2 = new FileInfo(path2);

fi1.CopyTo(path2);

Console.WriteLine("{0} was copied to {1}.", path, path2);

// MoveTo Method

string path = @ "d:\NewFile1.txt";

string path2 = @ "E:\NewFile1.txt";

FileInfo fi1 = new FileInfo(path);

FileInfo fi2 = new FileInfo(path2);

fi1.MoveTo(path2);

Console.WriteLine("{0} was moved to {1}.", path, path2);

//ApendText Method

FileInfo fi = new FileInfo(@ "E:\NewFile1.txt");

StreamWriter sw = fi.AppendText();

sw.WriteLine("This");

sw.WriteLine("is Extra");

sw.WriteLine("Text");
REGISTER NO : 1612075 NAME: P.Pooja

Console.WriteLine("File has been appended");

sw.Close();

// Opentext Method

FileInfo fi = new FileInfo(@ "E:\NewFile1.txt");

StreamReader sr = fi.OpenText();

string s = "";

while ((s = sr.ReadLine()) != null) {

Console.WriteLine(s);

3. What is serialization? List the types of Serialization Formatter and explain it with
an example program also define deserialization. CO3

Serialization is a mechanism of converting the state of an object into a byte stream.


Deserialization is the reverse process where the byte stream is used to recreate the actual Java
object in memory. This mechanism is used to persist the object.

The byte stream created is platform independent. So, the object serialized on one platform can be
deserialized on a different platform.
REGISTER NO : 1612075 NAME: P.Pooja

To make a Java object serializable we implement the java.io.Serializable interface.


The ObjectOutputStream class contains writeObject() method for serializing an Object.
public final void writeObject(Object obj)
throws IOException
The ObjectInputStream class contains readObject() method for deserializing an object.
public final Object readObject()
throws IOException,
ClassNotFoundException

Advantages of Serialization
1. To save/persist state of an object.
2. To travel an object across a network.

Only the objects of those classes can be serialized which are


implementing java.io.Serializable interface.
Serializable is a marker interface (has no data member and method). It is used to “mark” java
classes so that objects of these classes may get certain capability. Other examples of marker
interfaces are:- Cloneable and Remote.
Points to remember
1. If a parent class has implemented Serializable interface then child class doesn’t need to
implement it but vice-versa is not true.
2. Only non-static data members are saved via Serialization process.
3. Static data members and transient data members are not saved via Serialization process.So, if
you don’t want to save value of a non-static data member then make it transient.
4. Constructor of object is never called when an object is deserialized.
5. Associated objects must be implementing Serializable interface.
Example :
class A implements Serializable{

// B also implements Serializable


// interface.
B ob=new B();
}
SerialVersionUID
The Serialization runtime associates a version number with each Serializable class called a
SerialVersionUID, which is used during Deserialization to verify that sender and reciever of a
serialized object have loaded classes for that object which are compatible with respect to
serialization. If the reciever has loaded a class for the object that has different UID than that of
corresponding sender’s class, the Deserialization will result in an InvalidClassException. A
Serializable class can declare its own UID explicitly by declaring a field name.
REGISTER NO : 1612075 NAME: P.Pooja

It must be static, final and of type long.


i.e- ANY-ACCESS-MODIFIER static final long serialVersionUID=42L;
If a serializable class doesn’t explicitly declare a serialVersionUID, then the serialization runtime
will calculate a default one for that class based on various aspects of class, as described in Java
Object Serialization Specification. However it is strongly recommended that all serializable
classes explicitly declare serialVersionUID value, since its computation is highly sensitive to
class details that may vary depending on compiler implementations, any change in class or using
different id may affect the serialized data.
It is also recommended to use private modifier for UID since it is not useful as inherited member.
serialver
The serialver is a tool that comes with JDK. It is used to get serialVersionUID number for Java
classes.
You can run the following command to get serialVersionUID
serialver [-classpath classpath] [-show] [classname…]

Example 1:
// Java code for serialization and deserialization
// of a Java object
import java.io.*;

class Demo implements java.io.Serializable


{
public int a;
public String b;

// Default constructor
public Demo(int a, String b)
{
this.a = a;
this.b = b;
}}
class Test
{
public static void main(String[] args)
{
Demo object = new Demo(1, "geeksforgeeks");
String filename = "file.ser";

// Serialization
try
{
REGISTER NO : 1612075 NAME: P.Pooja

//Saving of object in a file


FileOutputStream file = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(file);

// Method for serialization of object


out.writeObject(object);

out.close();
file.close();

System.out.println("Object has been serialized");


}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
Demo object1 = null;

// Deserialization
try
{
// Reading the object from a file
FileInputStream file = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(file);

// Method for deserialization of object


object1 = (Demo)in.readObject();

in.close();
file.close();

System.out.println("Object has been deserialized ");


System.out.println("a = " + object1.a);
System.out.println("b = " + object1.b);
}

catch(IOException ex)
{
System.out.println("IOException is caught");
}

catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException is caught");
}
REGISTER NO : 1612075 NAME: P.Pooja

}
}
Output :
Object has been serialized
Object has been deserialized
a=1
b = geeksforgeeks

4. Develop a C# Console Application to get the employee details such as name, ID,
Basic Pay, DA from the user and calculate the net salary of the employee and also
write these information into a file using StreamWriter Class and read the same data
using StreamReader Class. CO3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EmployeeIndexer
{
class Program
{
static void Main(string[] args)
{
while (true)

Console.Clear();

Console.WriteLine("Menu");

Console.WriteLine("====");

Console.WriteLine("1.Add an Employee Record");

Console.WriteLine("2.Display Employee Record(s)");

Console.WriteLine("3.Exit");

Console.WriteLine("Enter your choice");

int ch = Int32.Parse(Console.ReadLine());

switch (ch)

{
REGISTER NO : 1612075 NAME: P.Pooja

case 1:

Employee_List emplist = new Employee_List();


emplist.AddEmployee();

break;

case 2:

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


{
Console.WriteLine();
}
break;
}} }}
class Employee{
string ename,edept;
int eno,esal;
public Employee()
{
ename="";
edept="";
eno=0;
esal=0;
}
public Employee(string name,string dept,int num,int sal)
{
ename=name;
edept=dept;
eno=num;
esal=sal;
}
public Employee this[int pos]
{
get
{
if(pos>0 && pos<=9){
return Employee;
}

You might also like