从文件读取数据到员工对象中,并对员工对象属性比较和修改:
在文件emp.txt中存有雇员信息,分别有字段emp_id(编号),emp_name(姓名),job(工作),salary(薪资)将雇员信息从文件中读出,放入雇员类Emp的实例中,并遍历所有成员当员工薪资小于2000时,增加工资500,并打印输出。emp.txt内容如下:
7369,SMITH,CLERK,800
7499,ALLEN,SALESMAN,2600
7521,WARD,SALESMAN,1250
7566,JONES,MANAGER,7839
7654,MARTIN,SALESMAN,1400
7698,BLAKE,MANAGER,2850

解决方案 »

  1.   

    package com.momo;public class Emp {
    private String emp_id;
    private String emp_name;
    private String job;
    private int salary; public Emp() {
    super();
    } public Emp(String emp_id, String emp_name, String job, int salary) {
    super();
    this.emp_id = emp_id;
    this.emp_name = emp_name;
    this.job = job;
    this.salary = salary;
    } public String getEmp_id() {
    return emp_id;
    } public void setEmp_id(String emp_id) {
    this.emp_id = emp_id;
    } public String getEmp_name() {
    return emp_name;
    } public void setEmp_name(String emp_name) {
    this.emp_name = emp_name;
    } public String getJob() {
    return job;
    } public void setJob(String job) {
    this.job = job;
    } public int getSalary() {
    return salary;
    } public void setSalary(int salary) {
    this.salary = salary;
    } @Override
    public String toString() {
    return "Emp [emp_id=" + emp_id + ", emp_name=" + emp_name + ", job="
    + job + ", salary=" + salary + "]";
    }
    }package com.momo;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 Test {
    public static void main(String[] args) {
    List<Emp> emps = new ArrayList<Emp>();

    try {
    BufferedReader bf = new BufferedReader(
    new FileReader("D://emp.txt"));
    String value = bf.readLine();
    while (null != value) {
    String[] values = value.split(",");
    Emp emp = new Emp();
    emp.setEmp_id(values[0]);
    emp.setEmp_name(values[1]);
    emp.setJob(values[2]);
    int salary = Integer.parseInt(values[3]);
    if (salary > 2000) {
    salary = salary + 500;
    }
    emp.setSalary(salary);
    emps.add(emp);
    value = bf.readLine();
    }
    System.out.println("--------------------");
    for (Emp emp : emps) {
    System.out.println(emp);
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
      

  2.   

    不知道对不对。此处省略Emp类
    public class ReaderEmp 
    {
        public static void main(String[] args)
        {
         ArrayList<Emp> list = new ArrayList<Emp>();
         list = readEmp();
        
         Iterator<Emp> it = list.iterator();
         while(it.hasNext())
         {
         Emp e = it.next();
         System.out.println(e.toString());
         }
        }
        
        public static ArrayList<Emp> readEmp()
        {
         BufferedReader br = null;
         ArrayList<Emp> list = new ArrayList<Emp>();
         try
         {
         br = new BufferedReader(new FileReader("emp.txt"));
        
         String line = null;
         while(null != (line = br.readLine()))
         {
         String[] str = line.split(",");
         if(Integer.parseInt(str[3]) < 2000)
         {
         str[3] = (Integer.parseInt(str[3]) + 500) + "";
         }
         Emp e = new Emp(Integer.parseInt(str[0]),str[1],str[2],Integer.parseInt(str[3]));
         list.add(e);
         }
        
         return list;
        
         }catch(Exception e)
         {
         throw new RuntimeException("读取文件错误");
         }
         finally
         {
         try
         {
         if(br != null)
         {
         br.close();
         }
         }catch(Exception e)
         {
         throw new RuntimeException("输入流关闭异常");
         }
         }
        }
        
        
    }
      

  3.   

    package view;import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.Vector; class Employee {
    //7369,SMITH,CLERK,800
    private String id;
    private String  name;
    private String dep;
    private float salary;
    public Employee(String id, String name, String dep, float salary) {
    super();
    this.id = id;
    this.name = name;
    this.dep = dep;
    this.salary = salary;
    }
    public Employee() {
    super();
    // TODO Auto-generated constructor stub
    }
    /**
     * @return the id
     */
    public String getId() {
    return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(String id) {
    this.id = id;
    }
    /**
     * @return the name
     */
    public String getName() {
    return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
    this.name = name;
    }
    /**
     * @return the dep
     */
    public String getDep() {
    return dep;
    }
    /**
     * @param dep the dep to set
     */
    public void setDep(String dep) {
    this.dep = dep;
    }
    /**
     * @return the salary
     */
    public float getSalary() {
    return salary;
    }
    /**
     * @param salary the salary to set
     */
    public void setSalary(float salary) {
    this.salary = salary;
    }
     }
    public class Compute{
        static Vector<Employee> employees=new Vector<Employee> ();
    public static void main(String[]args){

    File f=new File("d:\\employee.txt");

    if(f.exists()){

    try {
    FileReader fr=new FileReader(f);
    BufferedReader br=new BufferedReader(fr);
    for(int i=0;br.ready();i++){
    String[] employee=br.readLine().split(",");
    employees.add(new Employee(employee[0],employee[1],employee[2],Float.valueOf(employee[3])));
    }
    //关闭文件流
         br.close();
         fr.close();
    } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

       //遍历所有成员当员工薪资小于2000时,增加工资500,并打印输出。

    for(int i=0;i<employees.size();i++){
       if(employees.get(i).getSalary()<2000){
       employees.get(i).setSalary(employees.get(i).getSalary()+500);
       }
       System.out.println(employees.get(i).getId()+" "+employees.get(i).getName()+" "+employees.get(i).getDep()+" "+employees.get(i).getSalary());
    }

    }
    else{
       System.out.println("该文件不存在");
    }

    }
    }