0% found this document useful (0 votes)
63 views

Handson - File Handling and Multithreading

The document contains code examples for performing various file I/O operations in Java, including reading data from a file, counting words in a file, persisting employee objects to a file, and retrieving an employee object from a file by ID. The code demonstrates how to use FileReader, BufferedReader, FileOutputStream, ObjectOutputStream, FileInputStream, and ObjectInputStream to read from and write to files while serializing and deserializing objects.

Uploaded by

Vamsi Gembali
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Handson - File Handling and Multithreading

The document contains code examples for performing various file I/O operations in Java, including reading data from a file, counting words in a file, persisting employee objects to a file, and retrieving an employee object from a file by ID. The code demonstrates how to use FileReader, BufferedReader, FileOutputStream, ObjectOutputStream, FileInputStream, and ObjectInputStream to read from and write to files while serializing and deserializing objects.

Uploaded by

Vamsi Gembali
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Retrieving Data from file

import java.io.*;

public class FileDemo


{
public static void main(String[] args)
{
try{
FileReader fileReader = new FileReader(new File("log.txt"));
int n;
StringBuilder stringBuilder = new StringBuilder();

while ((n = fileReader.read())!= -1){


stringBuilder.append((char) n);
}
System.out.println(stringBuilder.toString());
}catch(IOException e){
System.out.println(e);
}
}
}

-----------------------------------------------------------------------------------
--------------------------
Counting the Word in the given file

import java.io.*;
import java.util.*;
import java.text.*;

public class FileDemo{


public static void main(String[] args)
{
try{
int cnt =0;
String s;
String[] buffer;
//File f1 = new File("log.txt");
//FileReader fr = new FileReader(f1);
BufferedReader bfr = new BufferedReader(new FileReader("log.txt"));

while((s=bfr.readLine())!=null) {
buffer = s.split(" ");
for(int i=0;i<buffer.length;i++){
if(buffer[i].equalsIgnoreCase("Knowledge")){
++cnt;
}
}
}
System.out.println(cnt);
}
catch(IOException e){
e.printStackTrace();
}

}
}
-----------------------------------------------------------------------------------
----------
Persist Employee

import java.io.Serializable;

public class Employee implements Serializable{

private int employeeId;


private String name;
private float appraisalRating;

public Employee(int empid, String name,float appraisalRating) {

this.employeeId = empid;
this.name = name;
this.appraisalRating=appraisalRating;
}

public int getEmployeeId() {


return employeeId;
}

public void setEmployeeId(int employeeId) {


this.employeeId = employeeId;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public float getAppraisalRating() {


return appraisalRating;
}

public void setAppraisalRating(float appraisalRating) {


this.appraisalRating = appraisalRating;
}
}

import java.io.*;
import java.util.ArrayList;

public class EmployeeUtility {


public boolean addEmployee(String fileName,ArrayList<Employee> employeeList){
try{
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
ObjectOutputStream objectOutputStream = new
ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(employeeList);

return true;
}
catch(IOException ignore){
}
return false;
}

public Employee viewEmployeeById(String fileName,int employeeId){


try{
FileInputStream fileInputStream = new FileInputStream(fileName);
ObjectInputStream objectInputStream = new
ObjectInputStream(fileInputStream);
ArrayList<Employee> employees = (ArrayList<Employee>)
objectInputStream.readObject();

for(Employee employee : employees){


if(employee.getEmployeeId() == employeeId){
return employee;
}
}
}
catch(IOException | ClassNotFoundException ignore){

}
return null;
}

import java.util.ArrayList;
import java.util.Arrays;

public class Main{


public static void main(String args[]){
Employee employee = new Employee(101,"xxx",9.5f);
Employee employee1 = new Employee(102,"xxxxx",8.5f);
ArrayList<Employee> employees = new
ArrayList<>(Arrays.asList(employee,employee1));
EmployeeUtility employeeUtility = new EmployeeUtility();
employeeUtility.addEmployee("database.emp",employees);
Employee employee2 = employeeUtility.viewEmployeeById("database.emp",101);
System.out.println(employee2.getName());
}
}

You might also like