Formatter类在每次写入文本文件后不会保留数据。

huangapple go评论134阅读模式
英文:

Formatter class doesn't keep data after each time writing on the text file

问题

  1. public class IoWrite
  2. {
  3. private static Formatter output; // 用于向文件输出文本的工具
  4. public static void main(String[] args)
  5. {
  6. openFile();
  7. addRecords();
  8. closeFile();
  9. }
  10. // 打开文件 clients.txt
  11. public static void openFile()
  12. {
  13. try
  14. {
  15. output = new Formatter("clients.txt"); // 打开文件
  16. }
  17. catch (SecurityException securityException)
  18. {
  19. System.err.println("写入权限被拒绝。终止程序。");
  20. System.exit(1); // 终止程序
  21. }
  22. catch (FileNotFoundException fileNotFoundException)
  23. {
  24. System.err.println("文件打开错误。终止程序。");
  25. System.exit(1); // 终止程序
  26. }
  27. }
  28. // 向文件添加记录
  29. public static void addRecords()
  30. {
  31. Scanner input = new Scanner(System.in);
  32. System.out.printf("%s%n%s%n? ",
  33. "输入账号、名字、姓氏和余额。",
  34. "输入文件结束指示符以结束输入。");
  35. while (input.hasNext()) // 循环直到文件结束指示符
  36. {
  37. try
  38. {
  39. // 将新记录输出到文件;假定输入有效
  40. output.format("%d %s %s %.2f%n", input.nextInt(),
  41. input.next(), input.next(), input.nextDouble());
  42. }
  43. catch (FormatterClosedException formatterClosedException)
  44. {
  45. System.err.println("写入文件出错。终止程序。");
  46. break;
  47. }
  48. catch (NoSuchElementException elementException)
  49. {
  50. System.err.println("无效的输入。请重试。");
  51. input.nextLine(); // 丢弃输入,以便用户重试
  52. }
  53. System.out.print("?");
  54. }
  55. }
  56. // 关闭文件
  57. public static void closeFile()
  58. {
  59. if (output != null)
  60. output.close();
  61. }
  62. } // end class CreateTextFile
英文:

I am developing a application that write on the text file. application write the data on the file correctly but if I restart the application and write new input it replace the new input to the old one that I write on file perversely, but what i need it is to keep the old input that i wrote before with the new one

  1. public class IoWrite
  2. {
  3. private static Formatter output; // outputs text to a file
  4. public static void main(String[] args)
  5. {
  6. openFile();
  7. addRecords();
  8. closeFile();
  9. }
  10. // open file clients.txt
  11. public static void openFile()
  12. {
  13. try
  14. {
  15. output = new Formatter("clients.txt"); // open the file
  16. }
  17. catch (SecurityException securityException)
  18. {
  19. System.err.println("Write permission denied. Terminating.");
  20. System.exit(1); // terminate the program
  21. }
  22. catch (FileNotFoundException fileNotFoundException)
  23. {
  24. System.err.println("Error opening file. Terminating.");
  25. System.exit(1); // terminate the program
  26. }
  27. }
  28. // add records to file
  29. public static void addRecords()
  30. {
  31. Scanner input = new Scanner(System.in);
  32. System.out.printf("%s%n%s%n? ",
  33. "Enter account number, first name, last name and balance.",
  34. "Enter end-of-file indicator to end input.");
  35. while (input.hasNext()) // loop until end-of-file indicator
  36. {
  37. try
  38. {
  39. // output new record to file; assumes valid input
  40. output.format("%d %s %s %.2f%n", input.nextInt(),
  41. input.next(), input.next(), input.nextDouble());
  42. }
  43. catch (FormatterClosedException formatterClosedException)
  44. {
  45. System.err.println("Error writing to file. Terminating.");
  46. break;
  47. }
  48. catch (NoSuchElementException elementException)
  49. {
  50. System.err.println("Invalid input. Please try again.");
  51. input.nextLine(); // discard input so user can try again
  52. }
  53. System.out.print("?");
  54. }
  55. }
  56. // close file
  57. public static void closeFile()
  58. {
  59. if (output != null)
  60. output.close();
  61. }
  62. } // end class CreateTextFile

答案1

得分: 0

你可以使用[FileWriter][1]来追加到现有文件。

openFile方法更新为:

  1. // 打开文件 clients.txt
  2. public static void openFile()
  3. {
  4. try
  5. {
  6. FileWriter fileWriter = new FileWriter("clients.txt", true);
  7. output = new Formatter(fileWriter); // 打开文件
  8. }
  9. catch (SecurityException securityException)
  10. {
  11. System.err.println("写入权限被拒绝。终止程序。");
  12. System.exit(1); // 终止程序
  13. }
  14. catch (FileNotFoundException fileNotFoundException)
  15. {
  16. System.err.println("打开文件时出错。终止程序。");
  17. System.exit(1); // 终止程序
  18. }
  19. }
  20. 注意`FileWriter`构造函数中我将`append`标志设置为true
  21. [1]: https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html
  22. <details>
  23. <summary>英文:</summary>
  24. You can use a [FileWriter][1] to append to existing file.
  25. Update `openFile` method as,
  26. // open file clients.txt
  27. public static void openFile()
  28. {
  29. try
  30. {
  31. FileWriter fileWriter = new FileWriter(&quot;clients.txt&quot;, true);
  32. output = new Formatter(fileWriter); // open the file
  33. }
  34. catch (SecurityException securityException)
  35. {
  36. System.err.println(&quot;Write permission denied. Terminating.&quot;);
  37. System.exit(1); // terminate the program
  38. }
  39. catch (FileNotFoundException fileNotFoundException)
  40. {
  41. System.err.println(&quot;Error opening file. Terminating.&quot;);
  42. System.exit(1); // terminate the program
  43. }
  44. }
  45. Note- In `FileWriter` constructor I have kept `append` flag as true
  46. [1]: https://docs.oracle.com/javase/7/docs/api/java/io/FileWriter.html
  47. </details>

huangapple
  • 本文由 发表于 2020年9月22日 18:37:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64007985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定