Java IO。从.txt文件加载为变量

时间:2013-06-18 13:55:14

标签: java save java-io

我正在制作一个简单的程序,我可以跟踪我的积蓄。我已经包括了周数,收入和保存的字段。我将此数据保存到文本文件,然后可以将其检索到文本区域。在关闭程序然后再次运行程序时,如何跟踪我的运行总计“总计”。我已经使用了一个数组作为一个运行总计,但是当我杀死它然后重新打开它时(显然),它会重新开始。

这是我的保存和阅读代码。总结一下,我希望能够将最后一个字段“total”作为变量检索,以便将其添加到新输入中。

    //get week and validate:
    strWeek = txtWeek.getText();
    if (strWeek.isEmpty())
    {
        JOptionPane.showMessageDialog(null, "Enter Week Number");
        return;
    }
    else
        week = Integer.parseInt(strWeek);

    //get income and validate:
    strIncome = txtIncome.getText();
    if (strIncome.isEmpty())
    {
        JOptionPane.showMessageDialog(null, "Enter Income");
        return;
    }
    else
        income = Double.parseDouble(strIncome);

    //get saved:
    strSaved = txtSaved.getText();
    if (strSaved.isEmpty())
    {
        JOptionPane.showMessageDialog(null, "Enter Saved");
        return;
    }
    else
        saved = Double.parseDouble(strSaved);

    total = total + saved;

    txtOutput.append(week + "\t" + income + "\t" + saved + "\t" + total + "\n");
    txtWeek.setText(null);
    txtIncome.setText(null);
    txtSaved.setText(null);

    try
    {
        File output = new File("C:\\savingsApp/Savings.txt");
        BufferedWriter outFile = new BufferedWriter (new FileWriter(output.getPath(), true));
        outFile.write(week + "\t" + income + "\t" + saved + "\t" + total);
        outFile.newLine();
        outFile.close();
        JOptionPane.showMessageDialog(null, "Savings updated"); 
    }

    catch (Exception e)
    {
        JOptionPane.showMessageDialog(null, "IO file error");
    }

 txtOutput.setText(null);
    //Declare:
    String incomingString = "";
    int counter = 0;
    //get file:
    try
    {
        File inputFile = new File ("C:\\savingsApp/savings.txt");
        BufferedReader inFile = new BufferedReader (new FileReader(inputFile));
        incomingString = inFile.readLine();
        while (incomingString != null)
        {
            counter++;
            txtOutput.append(incomingString + "\n");
            incomingString = inFile.readLine();
        }

    }
    catch (Exception e)
    {
        JOptionPane.showMessageDialog(null, "error loading file");
    } 

2 个答案:

答案 0 :(得分:0)

runningTotal设为ArrayList并保存到单独的文件,然后再杀死您的程序。这可以是逗号(或制表符)分隔形式。例如runningTotal.csv可能如下所示:

  

150,200,50,...,300

当您在savings.txt中阅读时,您还可以阅读runningTotal.csv,然后在ArrayList的末尾添加更多总计。当您再次关闭该程序时,请更新runningTotal.csv

答案 1 :(得分:0)

我假设您真正要求的是一种解析您从文件中获取的文本的方法。实现这一目标的方法可以是使用 java.util.Scanner ,例如:如下所示:

private Map<String, String> parseData(String text) {
  Map<String, String> data = new HashMap<String, String>();
  Scanner scanner = new Scanner(text);
  data.put("week", scanner.next());
  data.put("income", scanner.next());
  data.put("saved", scanner.next());
  data.put("total", scanner.next());
  return data;
}

然后你可以这样做:

...
while (incomingString != null) {
  counter++;
  String totalStr = parseData(incomingString).get("total");
  txtOutput.append(incomingString + "\n");
  incomingString = inFile.readLine();
}