write an employee class storing above mentioned information ,employee id, employee salary name,title write a piece of code that creates an arraylist of 100 objects of employee and store the information in the file wrte a method that reads the above stored information from file and prints the employee detail
public class Employee {
private int id;
private double salary;
private String name;
private String title;
public Employee(int id, double salary, String name, String title) {
this.id = id;
this.salary = salary;
this.name = name;
this.title = title;
}
@Override
public String toString() {
return id + ";" + salary + ";" + name + ";" + title;
}
}
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
public class Main {
static void readData(String fileName) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)))) {
String data;
while ((data = reader.readLine()) != null) {
System.out.println(data);
}
} catch (Exception e) {
}
}
public static void main(String[] args) {
ArrayList<Employee> employees = new ArrayList<>();
for (int i = 0; i < 100; i++) {
employees.add(new Employee(i, i * 10, Integer.toString(i), Integer.toString(i)));
}
try (PrintWriter writer = new PrintWriter("data.txt")) {
for (Employee employee : employees) {
writer.println(employee);
}
} catch (Exception e) {
}
readData("data.txt");
}
}
Comments
Leave a comment