多次读取包含相同条目的文件,但只想更改其中一个条目

时间:2019-06-24 18:56:57

标签: java bufferedreader

BufferedReader正在浏览一个文件,该文件包含各种,这些行都包含一个特定的标识符(即qdr)。 qdr包含特定数量。可以手动减少该数量。当减少数量时,BufferedReader将遍历文件并检查包含特定qdr的行。

它应该做的是仅在它发现的包含qdr的第一行中更改该金额,并且仅当减少的金额大于特定行的规定金额(金额<1)时,才应该更改该金额从同一qdr标识的下一行减少该金额。

我希望我能澄清我的问题。非常感谢您的帮助!

通过bufferedreader,正在检查文件是否包含给定的qdr。如果是这种情况,它将给定的金额添加到该行之一。但是,正如您在代码中看到的那样,它正在对包含该qdr的所有行执行此操作。我的想法是真正地逐行执行循环,并检查每行是否包含该qdr,如果不是,则转到下一行,如果是这种情况,请查看之后的数量是否大于0用新金额更改。如果不是,则查找包含该qdr的下一行并执行相同的操作。不幸的是,这是我挣扎的地方。

  public static void removeDB(String qdr, int amount, String editor, String date) {            
  File readFile = new File(databaseRead);
  File writeFile = new File(databaseWrite);
  // creating the line variable to read through lines in while loop
  String line = null;
  // go through file
try {
    // create FileReader which reads the file
    FileReader fileReader = new FileReader(readFile);
    // wrap the FileReader into BufferedReader
    BufferedReader buffRead = new BufferedReader(fileReader);
    // FileWriter to write lines into new file
    FileWriter writer = new FileWriter(writeFile);
    BufferedWriter buffWrite = new BufferedWriter(writer);

    // go through all lines
    while ((line = buffRead.readLine()) != null) {

        if (line.contains(qdr)) {
        // set the amount 
        String blankSpaceAmount = clearOutput.split(", ")[3];
        // setting new Amount
        int remainingAmount = Integer.parseInt(blankSpaceAmount) - amount;

        // if amount is bigger than 0, show qdr in DB with new amount
        if (remainingAmount > 0) {
        // set the remaining Amount to String to show it in text field in Remove GUI
        remAmount = String.valueOf(remainingAmount);
        // read line and replace the current amount String with remaining amount String
        String amountReplacement = line.replaceAll(blankSpaceAmount, remAmount);
        }

1 个答案:

答案 0 :(得分:1)

您可以做的一件事是使用一个布尔标志,表明您已经修改了第一个遇到的 qdr ,也许将其命名为 qdrChanged 。在您的 while 循环上方声明它,并将其初始化为 false

boolean qdrChanged = false;

然后在while循环中:

// go through all lines
while ((line = buffRead.readLine()) != null) {
    if (line.contains(qdr) && !qdrChanged) {
        // set the amount but only if this hasn't been done already.
        String blankSpaceAmount = clearOutput.split(", ")[3];
        // setting new Amount
        int remainingAmount = Integer.parseInt(blankSpaceAmount) - amount;

        // if amount is bigger than 0, show qdr in DB with new amount
        if (remainingAmount > 0) {
            // set the remaining Amount to String to show it in text field in Remove GUI
            remAmount = String.valueOf(remainingAmount);
            // read line and replace the current amount String with remaining amount String
            String amountReplacement = line.replaceAll(blankSpaceAmount, remAmount);
        }
        // .... The rest of code for the: if (line.contains(qdr) && !qdrChanged) { code block .... 

        qdrChanged = true;  // Flag that qdr has been modified

    } // END of Code block for:  if (line.contains(qdr) && !qdrChanged) {      

    // The rest of your WHILE loop code
}     

无论如何,可以达到这种效果。

无论您做什么,都不要使用break;来打破循环。这也会停止写入新文件。

在使用 Integer.parseInt()之前,请验证提供给该方法的字符串,以避免出现 NumberFormatException 的可能性:

/* If blankSpaceAmount is NOT a valid signed or unsigned
   integer value then make it a default value like 0 or 
   String.valueOf(amount) or whatever.
*/
if (!blankSpaceAmount.matches("-?\\d+") {
    blankSpaceAmount = "0";  // or throw a message or an exception if you like.
}  
int remainingAmount = Integer.parseInt(blankSpaceAmount) - amount;