简体   繁体   English

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

[英]reading through a file which contains the same entries multiple times, but only want to change one of those entries

The BufferedReader is going through a file which contains various lines that all contain a specific identifier (so called qdr) . BufferedReader正在遍历一个文件,该文件包含各 ,每行都包含一个特定的标识符(所谓的qdr) A qdr contains a specific amount. qdr包含特定数量。 This amount can be reduced manually. 可以手动减少该数量。 When reducing the amount the BufferedReader goes through the file and checks for the lines that contain a specific qdr. 当减少数量时,BufferedReader将遍历文件并检查包含特定qdr的行。

What it should do is to change that amount only in the first line it finds that contains that qdr and only if an amount is reduced that is bigger than the stated amount of a specific line (amount < 1), then it should reduce that amount from the next line that is identified by the same qdr. 它应该做的是仅在它发现的包含qdr的第一行中更改该金额,并且仅当减少的金额大于特定行的规定金额(金额<1)时,才应减少该金额从下一个由相同qdr标识的行开始。

I hope I could clarify my problems. 我希望我可以澄清我的问题。 Thank you very much for your help! 非常感谢您的帮助!

Via the bufferedreader it is being checked whether the file contains the given qdr. 通过bufferedreader,正在检查文件是否包含给定的qdr。 If it is the case it is adding the given amount to the one of the line. 如果是这种情况,它将给定的金额添加到该行之一。 However, as you can see in the code, it is doing it to all the lines that contain that qdr. 但是,正如您在代码中看到的那样,它正在对包含该qdr的所有行执行此操作。 My idea would be to really go line by line with a loop and check whether each line is containing that qdr and if it is not the case then go to the next line, if it is the case see if the amount would be > 0 after altering with the new amount. 我的想法是真正地逐行执行循环,并检查每行是否包含该qdr,如果不是,则转到下一行,如果是这种情况,请查看之后的数量是否大于0用新金额更改。 If not, then look for the next line containing that qdr and do the same. 如果不是,则查找包含该qdr的下一行并执行相同的操作。 Unfortunately, this is where I struggle. 不幸的是,这是我奋斗的地方。

  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);
        }

One thing you could do is use a boolean flag, to indicate that you have modified the first encountered qdr , perhaps name it qdrChanged . 您可以做的一件事是使用一个boolean标志,以表明您已经修改了第一个遇到的qdr ,也许将其命名为qdrChanged Declare it above your while loop and initialize it as false : while循环上方声明它,并将其初始化为false

boolean qdrChanged = false;

then in the while loop: 然后在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
}     

Anyways, something to this effect. 无论如何,某种效果。

Whatever you do, don't use break; 无论您做什么, 都不要使用break; to break out of the loop. 打破循环。 This will also stop the write of your new file. 这也将停止写入新文件。

Before using Integer.parseInt() validate the string supplied to the method so as to avoid the possibility of a NumberFormatException : 在使用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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM