从文本文件中读取保存并将其打印成文本

时间:2017-02-11 17:11:44

标签: java file input

我希望有人可以帮助我。

我正在尝试创建一个程序来从输入文件中读取变量到输出文件 输入文件如下是员工的名字,两个双精度值,卖方工资和他/她卖出的总价值。

JOAO
450.00
1230.30

FDJSI
333.00
2.00

MAJDIIDFH
433.00
222.50

输出所需卖家的总薪水

这是我一直试图制作的代码

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/**
 * A simple example program that reads a text file line by line and display each line.
 */
public class Salary {
public static void main(String[] args) {

    BufferedReader br = null;


    try {
        br = new BufferedReader(new FileReader("temp.txt"));
        String sellerName;


        while ((sellerName = br.readLine()) != null) {
            String  salary = br.readLine(); 
            String totalSale =br.readLine(); 
            double percentage  = 0.15;

            double  SaleAfterPercentage = totalSale * percentage; 
            //value of the total salary 
            double  finalSalary = salary +  SaleAfterPercentage ; 


        System.out.println(sellerName);
      //  System.out.println(salary);
     //    System.out.println(totalSale);
        }


    } catch (IOException e) {
        e.printStackTrace();

    } finally {
        try {
            if (br != null) {
                br.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个

BufferedReader br = null;
try {
    br = new BufferedReader(new FileReader("temp.txt"));
    String line = null;


    while ((line = br.readLine()) != null) {
        StringTokenizer  st = new StringTokenizer(line);

        String sellerName = st.nextToken();
        String salary = st.nextToken(); 
        String totalSale =st.nextToken(); 
        double percentage  = 0.15;

        double  SaleAfterPercentage = (Double.parseDouble(totalSale)) * percentage; 
        //value of the total salary 
        double  finalSalary = Double.parseDouble(salary) +  SaleAfterPercentage ; 


    System.out.println(sellerName);

    System.out.println(finalSalary);
     System.out.println(totalSale);
    }


} catch (Exception e) {
    e.printStackTrace();

} finally {
    try {
        if (br != null) {
            br.close();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        }
    }

阅读本文

JOAO 450.00 1230.30
FDJSI 333.00 2.00
MAJDIIDFH 433.00 222.50