在Java中写入文件的最佳方法是什么?

时间:2014-07-17 00:20:21

标签: java file-io

最近,我的一位朋友让我为他编程,这样可以更轻松地管理他的小生意。基本上,用户应该选择客户端的名称,并且代码检索有关客户端的信息,例如客户端欠下的金额,或者下次我的朋友应该与此客户端会面时。现在,我的程序通过使用这一小段代码从AccountData.txt文件中读取得很好...

        myScanner = new Scanner(System.in);
        diskScanner = new Scanner(AccountData);

        System.out.println("Type an existing account name to get started or type 'new' to create another");

        name = myScanner.next();

        do {
            reading = diskScanner.next();
        } while(!reading.equals(name));


        weekday = diskScanner.next();
        day = diskScanner.nextInt();
        month = diskScanner.nextInt();
        Amount = diskScanner.nextDouble();
        TotalAmount = diskScanner.nextDouble();

        System.out.print("Next Job is ");
        System.out.print(weekday);
        System.out.print(" ");
        System.out.print(month);
        System.out.print("/");
        System.out.println(day);
        System.out.print("The total amount payed by this client is ");
        System.out.println(TotalAmount);
        System.out.print("The total amount owed is ");
        System.out.println(Amount);

         AddDates.AddingDates();

...但我在写入现有代码时遇到了问题。

public class AddDates {
     private static Scanner myScanner;
     static String reply;
          public static void AddingDates() throws IOException {
              myScanner = new Scanner(System.in);
              System.out.println("Add Dates?");
              reply = myScanner.next();
              AccountReader.adder = new PrintWriter(new FileWriter(AccountReader.AccountData, true));

               if (reply .equals("Y")) {
                    System.out.println("Day, Date, Month, Cost of Lawn");
                    AccountReader.weekday = myScanner.next();
                    AccountReader.day = myScanner.next();
                    AccountReader.month = myScanner.next();
                    AccountReader.Amount = myScanner.next();

                    AccountReader.adder.print(" ");
                    AccountReader.adder.print(AccountReader.weekday);
                    AccountReader.adder.print(" ");
                    AccountReader.adder.print(AccountReader.day);
                    AccountReader.adder.print(" ");
                    AccountReader.adder.print(AccountReader.month);
                    AccountReader.adder.print(" ");
                    AccountReader.adder.print(AccountReader.Amount);
                    AccountReader.adder.print(" ");
                    AccountReader.adder.print(AccountReader.TotalAmount);
                    AccountReader.adder.flush();
                } else {
                     System.out.println("OK");
                }
      }

}

因为从文件中读取位置敏感,所以写入文件的这段代码不起作用。这主要是因为它将信息添加到文件的末尾。 (我还应该补充一点,AccountData.txt文件看起来像这样)

Masterson Saturday 22 9 50 300
Johnson Sunday 17 8 20 140 Sunday 24 8 20 140
//Etc.

所以我的主要问题是......无论如何要写入文件并将信息写入文件中的特定位置(比如其他字符串旁边)或者是否有其他方式我应该编写我的代码来制作它适合我的目的吗?

2 个答案:

答案 0 :(得分:1)

client like the amount this client owes,

如果您没有编程方面的经验,我强烈建议您远离这一点,直到您对自己的工作有一个更坚定的把握,因为处理金钱会变得极其危险,而且您的代码中的一个小错误可能会导致很多错误损坏。

现在我还没有完全了解你想要做什么。你想从

改变文件中的文本吗?
Masterson Saturday 22 9 50 300

Masterson Saturday 22 9 50 300 --> Processed Successfully!

在这种情况下,您必须在文件上打开输入和输出流,只需编写输出流,直到您到达要修改的行,打印行,然后继续将输入流放入输出流。如果我确切地知道你在找什么,我可以为它做一些示例代码

答案 1 :(得分:0)

请做在进行货币/货币计算时使用双打。请参阅Why not use Double or Float to represent currency?了解原因。

快速摘要 - "花车和双打不能准确地代表我们用于赚钱的10倍数。" ..."代表金钱作为双倍或浮动可能一开始看起来很好,因为软件会对微小的错误进行补偿,但是当你对不精确的数字进行更多的加法,减法,乘法和除法时,你就可以了错误加起来会失去越来越多的精确度。这使浮动和双打不足以处理金钱,其中需要基本10倍数倍的完美准确度。"