[英]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.