JAVA Streams and Writers:
FILE HANDLING INTRODUCTION
Provided "input.csv" which has User details. Read all the user information stored in CSV format and create a user object by parsing the line. Add all the user objects to the ArrayList. At last, display the user list.
Output format:
Use "%-15s %-20s %-15s %s\n" to print statements for the heading of the details in the Main method.
Use "%-15s %-20s %-15s %s\n" to print statements for the heading of the details in the Main method.
Sample Output :
Name Email Username Password
William will@gmail.com william will123
john john@gmail.com john abc
Name Email Username Password
William will@gmail.com william will123
john john@gmail.com john abc
CODE:
public class User{
private String name;
private String email;
private String username;
private String password;
public User(){}
public User(String name, String email, String username, String password) {
super();
this.name = name;
this.email = email;
this.username = username;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password=password;
}
}
import java.io.*;
import java.util.*;
public class UserBO {
List<User> list = new ArrayList<User>();
String str = new String();
public List<User> readFromFile(BufferedReader br) throws IOException {
while ((str = br.readLine()) != null) {
// str=str+br.readLine();
String records[] = str.split("\n");
for (int i = 0; i < records.length; i++) {
String values[] = (records[i]).split(",");
// System.out.println(values[0]);
User user = new User();
user.setName(values[0]);
user.setEmail(values[1]);
user.setUsername(values[2]);
user.setPassword(values[3]);
list.add(user);
values = null;
}
}
return list;
}
public void display(List<User> list) {
for(User user: list) {
System.out.printf("%-15s %-20s %-15s %s\n", user.getName(), user.getEmail(), user.getUsername(), user.getPassword());
}
}
}
import java.io.BufferedReader;
//import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
List<User> userList = null;
FileReader fr = new FileReader("input.csv");
BufferedReader br = new BufferedReader(fr);
UserBO ub = new UserBO();
try {
userList = ub.readFromFile(br);
//System.out.println(userList.size());
} catch (Exception e) {
System.out.println(e);
}finally {
br.close();
}
if(userList.isEmpty()) {
System.out.println("The list is empty");
}else {
String name = "Name", email="Email", username="Username", password="Password";
System.out.printf("%-15s %-20s %-15s %s\n", name, email, username, password);
ub.display(userList);
}
}
}
Comments
Post a Comment