[英]Replace Line in .txt File based off of Flag
至少我希望,這個問題不同於通常的“我需要替換一行代碼”問題。
我正在尋找一個文本文件,在名為account.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
}
這里有一些事情要考慮。
如果文件很小,則可以將整個內容讀入一個字符串數組中(請查看Java 7 Files類的javadocs)。 然后,您可以前后移動數組以進行更改。 然后將修改后的文件寫回。
如果文件很大,則可以一次從輸入讀取並向一行寫入臨時文件(但是將輸出延遲一行,以便可以觸發輸入標志)。 然后刪除舊的輸入文件並重命名臨時文件。
這是一種方法。
工藝流程 :
-將文件的所有行寫入ArrayList
-如果找到標志,則標記該行號
-如果您的行號不是-1,則找到該帳戶,然后更改ArrayList
並將所有行寫回到文件中。
public boolean modifyBalance(int accountNum, int newBalance)
{
int lineNumberOfAccount = -1;
boolean foundFlag = false;
BufferedReader file = null;
List<String> fileLines = new ArrayList<String>();
try
{
FileReader fileReader = new FileReader("accounts.txt");
file = new BufferedReader(fileReader);
String line;
String input = "";
String flag;
Integer intInstance = accountNum;
flag = intInstance.toString();
int lineNumber = 0;
while ((line = file.readLine()) != null)
{
fileLines.add(line);
System.out.println(lineNumber + "] " + line);
// input += line;
if (line.equals(flag))
{
lineNumberOfAccount = lineNumber;
foundFlag = true;
} // end if
lineNumber++;
} // end while
} // end try
catch (IOException e)
{
System.out.println("Input Failure in Modify Balance of Account" + " Repository.");
// don't exit here, you are returning false
// System.exit(0);
return false;
}
// Close the file handle here
finally
{
if (file != null)
{
try
{
file.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
// look up inObj in the text file and change the associated
// balance to newBalance
System.out.println("lineNumberOfAccount: " + lineNumberOfAccount);
// found the account
if (lineNumberOfAccount != -1)
{
int nextLine = lineNumberOfAccount + 1;
// remove the old balance
fileLines.remove(nextLine);
// add the new balance
fileLines.add(nextLine, String.valueOf(newBalance));
System.out.println(fileLines);
// write all the lines back to the file
File fout = new File("accounts.txt");
FileOutputStream fos = null;
BufferedWriter bw = null;
try
{
fos = new FileOutputStream(fout);
bw = new BufferedWriter(new OutputStreamWriter(fos));
for (int i = 0; i < fileLines.size(); i++)
{
bw.write(fileLines.get(i));
bw.newLine();
}
}
catch (IOException e)
{
System.out.println("Could not write to file");
return false;
}
// Close the file handle here
finally
{
if (bw != null)
{
try
{
bw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
return foundFlag;
}
注意事項 :
System.exit()
時要小心,我在代碼中對此進行了注釋,因為如果您遇到IOException
則可能不希望以這種方式退出程序。 您還可以引發異常或將其包裝在RuntimeException
然后由調用代碼對其進行處理。 newBalance
變量為double
而不是int
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.