【问题标题】:Reading data from a text file to compute and write the data in another text file从文本文件中读取数据以计算和写入另一个文本文件中的数据
【发布时间】:2016-02-01 21:18:32
【问题描述】:

文件 Employee.txt 包含两种类型员工的员工详细信息,即按月支付和按小时支付。该文件包含名字、姓氏、性别、职级、类型和基本工资(如果是按月支付的员工),或者是小时费率和工作小时数(如果是按小时支付的员工)。文件示例如下:

John Smiths M 经理每月 45000.00

Sunil Bates M 高级每小时 700.00 45

Eva Leung F 官月 30500.00

我必须编写一个程序来查看每个员工并计算奖金占基本工资的百分比。对于按小时计酬的雇员,基本工资是按小时工资乘以当月工作小时数。如果军衔是“军官”,奖金率为20%,如果军衔为高级或以上,奖金率为15%。该程序应将每个员工的完整详细信息写入一个新文件 TotalSalary.txt,即名字、姓氏、性别和总工资(即奖金)。

这是我的解决方法:

将员工数据写入文本文件类

public class files_qu1 {

public static Formatter writein;
private static Scanner sc;

public static void main(String[] args) {
    // TODO Auto-generated method stub

    try{
        writein= new Formatter("Employees.txt");
    }

    catch(SecurityException se){
        System.out.println("You do have access to this file");
        System.exit(1);
    }

    catch(FileNotFoundException fnfe){
        System.out.println("Error in creating file");
        System.exit(1);
    }

    Scanner sc= new Scanner(System.in);

    String fname, lname, gender, rank, type;
    double salary, t_sal, hours=0.0;
    int num;


    System.out.println("Enter num of employees");
    num= sc.nextInt();

    for(int i=0; i<num; i++){
        System.out.println("First name: ");
        fname=sc.next();

        System.out.println("Last name: ");
        lname=sc.next();

        System.out.println("Gender: ");
        gender=sc.next();

        System.out.println("Rank: ");
        rank=sc.next();

        System.out.println("Type: ");
        type=sc.next();

        System.out.println("Salary: ");
        salary=sc.nextDouble();

        if(type.equals("hourly")){
            System.out.println("Hours worked: ");
            hours=sc.nextInt();

        }


        writein.format(" %s %s %s %s %s %.2f %.2f\n",fname, lname, gender, rank, type, salary, hours);
    }

    sc.close();
    if(writein!=null){
        writein.close();
         }


    }

}

从员工文本文件中读取和计算数据到 TotalSalary 文本文件类

public class files_qu1read {

private static Formatter writeToFile;
private static Scanner sc;

public static void main(String[] args) {
    // TODO Auto-generated method stub

    try {
        sc= new Scanner(new File("C:/Users/Diksha/workspace/Java_Sem1/Employees.txt"));
    }

    catch
        (FileNotFoundException fnfe){
            System.out.println("Error Creating File");
            System.exit(1);
    }

    String fname = null, lname, gender, rank, type;
    double salary, t_sal = 0, hours;


    while(sc.hasNext()){
        fname=sc.next();
        lname=sc.next();
        gender=sc.next();
        rank=sc.next();
        type=sc.next();
        salary=sc.nextDouble();
        hours=sc.nextDouble();

        if(type.equals("hourly")){

            t_sal= salary*hours;

        }

        if(type.equals("monthly")&&rank.equals("officer")){
            t_sal= salary*1.2;

        }

        if(type.equals("monthly")&&(rank.equals("senior")||rank.equals("manager"))){
            t_sal= salary*1.15;

        }

        try{
            writeToFile = new Formatter("TotalSalary.txt");
        }
        catch (SecurityException se) {
            System.out.println("You do have access to this file");
            System.exit(1);
        }
        catch (FileNotFoundException fnfe) {
            System.out.println("Error Creating File");
            System.exit(1);
        }


        writeToFile.format("First name: %s Last name: %s Gender: %s Total Salary: %.2f",fname,lname, gender, t_sal);
    }



    sc.close();
    if(writeToFile!=null){
        writeToFile.close();
         }


    }

 }

基本上,一切正常,程序能够写入员工文本文件,但在读取和写入数据到 TotalSalary 文件时,只有我写入员工文件的最后一个员工的姓名是写入 TotalSalary 文件以及他的总工资。

即如果Employees.txt文件有这样的数据:

Riley Fox f 经理每月 45000.00 0.00

Emily Roth f 军官每小时 10000.00 8.00

在 TotalSalary.txt 中,唯一显示的数据是最后一位员工的数据:

名字:Emily 姓氏:Roth 性别:f 总薪水:80000.00

【问题讨论】:

  • 我从没用过Formatter 写东西,但this answer 可能对你有帮助。
  • @StepTNT 非常感谢!

标签: java file


【解决方案1】:

每次创建要写入的格式化程序时,都会创建或覆盖文件。所以任何内容都会丢失。请阅读 Javadoc:

  • @param 文件名
    • 用作此目标的文件的名称
    • 格式化程序。 **如果文件存在,那么它将被截断为
    • 大小为零;否则,将创建一个新文件**。输出
    • 将被写入文件并被缓冲。

您可以在循环外创建格式化程序

 try{
        writeToFile = new Formatter("TotalSalary.txt");
    }
    catch (SecurityException se) {
        System.out.println("You do have access to this file");
        System.exit(1);
    }
    catch (FileNotFoundException fnfe) {
        System.out.println("Error Creating File");
        System.exit(1);
    }
while(sc.hasNext()){

...

并重复使用它直到程序结束并最终关闭它。您始终可以使用 try-with-resources 格式。

【讨论】:

  • 非常感谢。这很有帮助:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-04
  • 1970-01-01
  • 2016-08-14
  • 1970-01-01
  • 2019-05-15
  • 1970-01-01
  • 2015-08-11
相关资源
最近更新 更多