基于off标志替换.txt文件中的行

mitkmikd  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(388)

这个问题不同于通常的“我需要替换一行代码”的问题,至少我希望如此。
我想在一个名为accounts.txt的文本文件中编辑一行代码,而不是使用该行作为替换的标志,我需要使用它上面的行,因为文件的进程是“account number,balance”。感谢您的帮助!这是我目前掌握的情况。

  1. public boolean modifyBalance(int accountNum, int newBalance) {
  2. try {
  3. FileReader fileReader = new FileReader("accounts.txt");
  4. BufferedReader file = new BufferedReader(fileReader);
  5. String line;
  6. String input = "";
  7. String flag;
  8. boolean foundFlag = false;
  9. Integer intInstance = accountNum;
  10. flag = intInstance.toString();
  11. while ((line = file.readLine()) != null) {
  12. input += line;
  13. if (line.equals(flag)) {
  14. file.readLine();
  15. input += "\n" + newBalance;
  16. foundFlag = true;
  17. }//end if
  18. }//end while
  19. return foundFlag;
  20. } //end try
  21. catch (IOException e) {
  22. System.out.println("Input Failure in Modify Balance of Account"
  23. + " Repository.");
  24. System.exit(0);
  25. return false;
  26. }
  27. // look up inObj in the text file and change the associated
  28. // balance to newBalance
  29. }
bgibtngc

bgibtngc1#

这里有一个方法。
过程:
-将文件的所有行写入 ArrayList -如果你找到了旗子,那就标记行号
-如果您的行号不是-1,则您找到了帐户,然后对 ArrayList 把所有的行写回文件。

  1. public boolean modifyBalance(int accountNum, int newBalance)
  2. {
  3. int lineNumberOfAccount = -1;
  4. boolean foundFlag = false;
  5. BufferedReader file = null;
  6. List<String> fileLines = new ArrayList<String>();
  7. try
  8. {
  9. FileReader fileReader = new FileReader("accounts.txt");
  10. file = new BufferedReader(fileReader);
  11. String line;
  12. String input = "";
  13. String flag;
  14. Integer intInstance = accountNum;
  15. flag = intInstance.toString();
  16. int lineNumber = 0;
  17. while ((line = file.readLine()) != null)
  18. {
  19. fileLines.add(line);
  20. System.out.println(lineNumber + "] " + line);
  21. // input += line;
  22. if (line.equals(flag))
  23. {
  24. lineNumberOfAccount = lineNumber;
  25. foundFlag = true;
  26. } // end if
  27. lineNumber++;
  28. } // end while
  29. } // end try
  30. catch (IOException e)
  31. {
  32. System.out.println("Input Failure in Modify Balance of Account" + " Repository.");
  33. // don't exit here, you are returning false
  34. // System.exit(0);
  35. return false;
  36. }
  37. // Close the file handle here
  38. finally
  39. {
  40. if (file != null)
  41. {
  42. try
  43. {
  44. file.close();
  45. }
  46. catch (IOException e)
  47. {
  48. e.printStackTrace();
  49. }
  50. }
  51. }
  52. // look up inObj in the text file and change the associated
  53. // balance to newBalance
  54. System.out.println("lineNumberOfAccount: " + lineNumberOfAccount);
  55. // found the account
  56. if (lineNumberOfAccount != -1)
  57. {
  58. int nextLine = lineNumberOfAccount + 1;
  59. // remove the old balance
  60. fileLines.remove(nextLine);
  61. // add the new balance
  62. fileLines.add(nextLine, String.valueOf(newBalance));
  63. System.out.println(fileLines);
  64. // write all the lines back to the file
  65. File fout = new File("accounts.txt");
  66. FileOutputStream fos = null;
  67. BufferedWriter bw = null;
  68. try
  69. {
  70. fos = new FileOutputStream(fout);
  71. bw = new BufferedWriter(new OutputStreamWriter(fos));
  72. for (int i = 0; i < fileLines.size(); i++)
  73. {
  74. bw.write(fileLines.get(i));
  75. bw.newLine();
  76. }
  77. }
  78. catch (IOException e)
  79. {
  80. System.out.println("Could not write to file");
  81. return false;
  82. }
  83. // Close the file handle here
  84. finally
  85. {
  86. if (bw != null)
  87. {
  88. try
  89. {
  90. bw.close();
  91. }
  92. catch (IOException e)
  93. {
  94. e.printStackTrace();
  95. }
  96. }
  97. }
  98. }
  99. return foundFlag;
  100. }

笔记:
您需要确保正在关闭文件句柄。
理想情况下,应该将此代码分解为至少两个方法。一种方法用于查找行号,另一种方法用于在找到帐户时将文件写回。
使用时要小心 System.exit() 我在我的代码中对此进行了注解,因为如果您得到一个 IOException . 也可以抛出异常或将其 Package 到 RuntimeException 让调用代码来处理。
你可以考虑 newBalance 变量必须是 double 而不是 int

展开查看全部
tzcvj98z

tzcvj98z2#

这里有一些事情要考虑。
如果文件很小,您可以将整个内容读入一个字符串数组(查看Java7文件类的javadocs)。然后你可以前后移动数组来进行更改。然后把修改过的文件写出来。
如果文件很大,您可以从输入中读取数据,然后一次向临时文件写入一行数据(但要将输出延迟一行,这样您就可以触发输入标志)。然后删除旧的输入文件并重命名临时文件。

相关问题