【发布时间】:2016-09-04 17:44:43
【问题描述】:
至少我希望这个问题不同于通常的“我需要替换一行代码”问题。
我正在寻找在名为 accounts.txt 的文本文件中编辑一行代码,而不是使用该行作为替换的标志,我需要使用它上面的行,因为文件的进展转到“帐号,余额”。任何帮助表示赞赏!这是我目前所拥有的。
public boolean modifyBalance(int accountNum, int newBalance) {
try {
FileReader fileReader = new FileReader("accounts.txt");
BufferedReader file = new BufferedReader(fileReader);
String line;
String input = "";
String flag;
boolean foundFlag = false;
Integer intInstance = accountNum;
flag = intInstance.toString();
while ((line = file.readLine()) != null) {
input += line;
if (line.equals(flag)) {
file.readLine();
input += "\n" + newBalance;
foundFlag = true;
}//end if
}//end while
return foundFlag;
} //end try
catch (IOException e) {
System.out.println("Input Failure in Modify Balance of Account"
+ " Repository.");
System.exit(0);
return false;
}
// look up inObj in the text file and change the associated
// balance to newBalance
}
【问题讨论】:
-
您的代码不会替换任何东西;它只读取。这个问题实际上是关于创建一个在找到帐户时返回
true的方法吗? -
尽管如此,您的问题只是关于寻找线路吗?
-
我没有写代码行来重写文件。我想搜索文本文件并将新行追加到它应该在的位置,但我从未放入方法的写入部分。
标签: java file text bufferedreader overwrite