【问题标题】:Erasing my data on the txt file while trying to add numbers in Java尝试在 Java 中添加数字时擦除 txt 文件中的数据
【发布时间】:2018-09-05 18:25:30
【问题描述】:

以下代码工作正常,除了应该将数字添加到现有 txt 文件 (Fred56.txt) 的代码。他发布了:

PrintWriter outputfile = new PrintWriter(file);
outputfile.println(totalDeposit);
outputfile.close();

我尝试搜索它,但在询问用户(扫描仪)文件名时找不到正确答案。我想将totalDeposit 添加到文件中的现有数据中,但不删除它们。

import java.io.*;
import java.util.Scanner;

public class TestSavingsAccount {

    public static void main(String[] args) throws IOException {

        double totalDeposit = 0;
        double totalInterest = 0 ;
        double totalWithdraw = 0;
        String filename ;

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter the annual interest rate");
        double userAnnualinterest = keyboard.nextDouble();

        System.out.println(" Enter the starting balance");
        double userStartingbalance = keyboard.nextDouble();

        System.out.println(" Enter the number of months");
        int userNumberMonths = keyboard.nextInt();

        SavingsAccount account1 = new SavingsAccount(userStartingbalance);
        account1.setAnnualInterest(userAnnualinterest);

        for(int currentmonth = 1 ; currentmonth <= userNumberMonths; currentmonth ++ )
        {
            System.out.println("How much did you deposit during the month ?" + currentmonth);
            double depositAmount = keyboard.nextDouble();

            account1.deposit(depositAmount);
            totalDeposit += depositAmount ;

            System.out.println("How much do you want to withdraw ?" + currentmonth);
            double userWithdraw = keyboard.nextDouble();
            account1.withdraw(userWithdraw);
            totalWithdraw += userWithdraw ;

            totalInterest += account1.addMonthlyInterest();


            keyboard.nextLine() ;

            System.out.println(" What is the file name ?");
            filename = keyboard.nextLine() ;
            File file = new File(filename);

            PrintWriter outputfile = new PrintWriter(file);
            outputfile.println(totalDeposit);
            outputfile.close();

        }

        System.out.println(" The final balance of the end " + userNumberMonths + "is" +
            account1.getBalance() + " total amount of withdraw " + totalWithdraw +
            " Total amount of deposit " +totalDeposit + " The total of interest" + totalInterest);

    }

}

【问题讨论】:

  • 我这样做了,但它仍然会擦除以前的数据。 { 键盘.nextLine() ; System.out.println("文件名是什么?");文件名 = 键盘.nextLine() ;文件文件 = 新文件(文件名); PrintWriter outputfile = new PrintWriter(file); outputfile.println(totalDeposit);输出文件.close(); }

标签: java file file-writing


【解决方案1】:

根据我的理解,您正在寻求以附加模式打开文件的解决方案。 有几种方法可以实现这一目标。您可以在代码中使用以下内容。

try{
   PrintWriter outputfile = new PrintWriter(new BufferedWriter(new FileWriter (filename, true)));
   outputfile.println(totalDeposit);
   outputfile.close();
   }catch(IOException ex){
// Handle your exception
   }

【讨论】:

  • 对象构造正在触发
  • 非常感谢。它解决了我的问题!我想知道从 Java 开始这本书给出了 SavingAccount 问题,然后要求将存款添加到现有文件中而不研究 try and catch 功能?
  • 您认为这是解决问题的唯一方法吗?我试图在循环外运行现有代码,但没有成功。
  • @Freddy53 不,这不是唯一的方法。您可以寻找以附加模式打开文件的不同方法。尝试读取 catch 块中的异常消息“ex”。它会给你在不同情况下遇到的错误。
  • 我在网上搜索过,实际上你需要你写的代码。我没有找到其他方法来做到这一点。奇怪的是,Gaddis 要求我们找到解决方案,甚至在他之前的章节中都没有谈到 catch and try。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 2021-10-06
  • 2020-10-13
  • 1970-01-01
  • 2013-01-23
  • 2016-06-11
  • 1970-01-01
相关资源
最近更新 更多