File handling introduction

 MAIN.JAVA:

import java.io.*;



import java.util.*;



public class Main {





       public static void main(String[] args) throws IOException{





       BufferedReader br=new BufferedReader(new FileReader("input.csv"));



       UserBO userbo=new UserBO();



       List<User> list=userbo.readFromFile(br);



       if(list.isEmpty())



       {



           System.out.println("The list is empty");



       }



       else



       {
    	   String name="Name";
           String email="Email";
           String username="Username";
           String password="Password";
           System.out.printf("%-15s %-20s %-15s %s\n",name,email,username,password);
           userbo.display(list);

       }

       }

   }




USER.JAVA:

import java.util.*;

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) {
	    //Fill your code here
		this.password= password;
	}
	


	}




USERBO.JAVA:

import java.io.*;
import java.util.*;
public class UserBO{



   public List<User> readFromFile(BufferedReader br) throws IOException



   {



       List<User> list=new ArrayList<User>();



       User user;





           String line;



           while((line=br.readLine())!=null)



           {



               String[] tokens=line.split(",");



               user=new User();



               user.setName(tokens[0]);



               user.setEmail(tokens[1]);



               user.setUsername(tokens[2]);



               user.setPassword(tokens[3]);



               list.add(user);



           }





       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());



      }



   }



}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.