[英]Replace Line in .txt File based off of Flag
this question is different from the usual "I need to replace a line of code" questions, at least I hope. 至少我希望,这个问题不同于通常的“我需要替换一行代码”问题。
I'm looking to edit a line of code in a text file called accounts.txt, and instead of using that line as the flag for the replacement, I need to use the line above it, since the progression of the file goes "Account Number, Balance". 我正在寻找一个文本文件,在名为account.txt的文本文件中编辑一行代码,而不是使用该行作为替换标志,而是需要使用它上面的行,因为文件的进度为“帐户数字,余额”。 Any help is appreciated!
任何帮助表示赞赏! Here's what I have so far.
到目前为止,这就是我所拥有的。
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
}
Here are some things to think about. 这里有一些事情要考虑。
If the file is small, you could read the whole thing into an array of strings (have a look at the javadocs for the Java 7 Files class). 如果文件很小,则可以将整个内容读入一个字符串数组中(请查看Java 7 Files类的javadocs)。 Then you can walk the array forwards and backwards to make your change.
然后,您可以前后移动数组以进行更改。 Then write the modified file back out.
然后将修改后的文件写回。
If the file is large you could read from the input and write to a temporary file a line at a time (but delay the output by a line so you can trigger off the input flag). 如果文件很大,则可以一次从输入读取并向一行写入临时文件(但是将输出延迟一行,以便可以触发输入标志)。 Then delete the old input file and rename the temporary.
然后删除旧的输入文件并重命名临时文件。
Here's one way to do it. 这是一种方法。
Process : 工艺流程 :
-Writes all the lines of the file to an ArrayList
-将文件的所有行写入
ArrayList
-If you find the flag, then mark that line number -如果找到标志,则标记该行号
-If your line number is not -1 you found the account, then make the change to the ArrayList
and write all the lines back to the file. -如果您的行号不是-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;
}
Notes : 注意事项 :
System.exit()
I commented this out in my code because you may not want to exit the program this way if you get an IOException
. System.exit()
时要小心,我在代码中对此进行了注释,因为如果您遇到IOException
则可能不希望以这种方式退出程序。 You can also throw the exception or wrap it in a RuntimeException
and let the calling code deal with it. RuntimeException
然后由调用代码对其进行处理。 newBalance
variable be a double
instead of an int
newBalance
变量为double
而不是int
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.