• GUI design using drag and drop feature of IDE(Net beans), • File IO
1. 1
LAB Report11
CS2104 - OBJECT ORIENTED PROGRAMMING (3+1)
BS-CS (1) OR BS-SE (2)
SPRING 2019
INSTRUCTOR(S)-IN-CHARGE: MR. BILAL MAQBOOL
Submitted by: Syed Shahrose Sohail
2. 2
Lab Number: 11
Lab Title: GUI design
Aim: To learn graphical user interface
Topics covered:
GUI design using drag and drop feature of IDE(Net beans),
File IO
Character Streams
FileReader,FileWriter
Byte Streams
File input stream, File output stream
Object input stream, Object output stream
Character vs Streams
Sterilization,Deserilization and Externalization
Lab Task 1: Write a JAVA program to read and display data (in console) stored in .txt file.
Code:
import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[]) throws IOException{
File f=new File("E:InputFile.txt");
FileInputStream InputF=new FileInputStream(f);
int a;
do{ a=InputF.read();
if(a!=-1){
System.out.print((char)a);}
}while (a!=-1);}}
3. 3
Lab Task 02: Write a JAVA program to get data from user and store in .txt file.
import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[]) throws IOException{
File f=new File("E:OutputFile.txt");
f.createNewFile();
FileOutputStream outputStream=new FileOutputStream(f,true);
String str="Your time is limited";
char arr[]=str.toCharArray();
}}
4. 4
Lab Task 3: Write a JAVA program to read and display data (in console) stored in .txt file.
Code:
import java.io.Serializable;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
class Employee implements Serializable {
private static final long serialVersionUID = -299482035708790407L;
private String name;
private String gender;
private int age;
private String role;
// private transient String role;
public Employee(String n) {
this.name = n;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getRole() {
return role;
}
public void setRole(String role) {
5. 5
this.role = role;
}
@Override
public String toString() {
return "Employee:: Name=" + this.name + " Age=" + this.age + " Gender=" +
this.gender + " Role=" + this.role;
}
}
public class shah {
public static void main(String[] args) {
Employee emp = new Employee("Pankaj");
emp.setAge(35);
emp.setGender("Male");
emp.setRole("CEO");
System.out.println(emp);
try {
FileOutputStream fos = new FileOutputStream("E://p.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
// write object to file
oos.writeObject(emp);
System.out.println("Done");
// closing resources
oos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}