【问题标题】:Reading from text file saving it and printing it out into a text从文本文件中读取并保存并打印成文本
【发布时间】:2017-02-11 17:11:43
【问题描述】:

我希望有人可以帮助我。

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

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

【问题讨论】:

  • 你只有一个 Reader 对象......请在 Stackoverflow 上询问之前对写入文件进行一些研究如何

标签: java file input


【解决方案1】:

试试这个

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

【讨论】:

    猜你喜欢
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    相关资源
    最近更新 更多