Java 从文本文件读取数据以计算数据并将其写入另一个文本文件
文件Employee.txt包含两类员工的员工详细信息,即月薪和小时工资。如果是月薪员工,则该文件包含名字、姓氏、性别、职级、类型和基本工资;如果是小时薪员工,则包含小时工资和工作小时数。该文件的示例如下所示: 约翰·史密斯M经理每月45000.00 Sunil Bates M高级每小时700.00 45 梁汝桦主任每月30500.00 我必须写一个程序,看看每个员工,计算奖金占基本工资的百分比。对于时薪员工,基本工资为时薪乘以当月工作小时数。如果军衔为“军官”,奖金率为20%;如果军衔为高级或以上,奖金率为15%。该计划应在新文件TotalSalary.txt中写入每位员工的全部详细信息,即姓名、姓氏、性别和工资总额(即奖金) 以下是我是如何计算出来的: 将员工数据写入文本文件类Java 从文本文件读取数据以计算数据并将其写入另一个文本文件,java,file,Java,File,文件Employee.txt包含两类员工的员工详细信息,即月薪和小时工资。如果是月薪员工,则该文件包含名字、姓氏、性别、职级、类型和基本工资;如果是小时薪员工,则包含小时工资和工作小时数。该文件的示例如下所示: 约翰·史密斯M经理每月45000.00 Sunil Bates M高级每小时700.00 45 梁汝桦主任每月30500.00 我必须写一个程序,看看每个员工,计算奖金占基本工资的百分比。对于时薪员工,基本工资为时薪乘以当月工作小时数。如果军衔为“军官”,奖金率为20%;如果军衔为高级或
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();
}
}
}
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
艾米丽·罗斯f警官每小时10000.00 8.00
在TotalSalary.txt中,显示的唯一数据是最后一名员工的数据,即:
名字:Emily姓氏:Roth性别:f总工资:80000.00每次创建要写入的格式化程序时,文件都会被创建或覆盖。因此,任何内容都会丢失。请阅读Javadoc:
- @参数文件名
- 要用作此文件目标的文件的名称
- 格式化程序**如果文件存在,则它将被截断为
- 零尺寸;否则,将创建一个新文件**。输出
- 将写入文件并进行缓冲
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()){
重复使用它直到程序结束,最后关闭它。您可以随时使用“资源试用”格式。我从来没有使用过
格式化程序
来编写内容,但可能可以帮助您。@StepTNT非常感谢!谢谢。这很有帮助:-)