如果条件总是失败,则追加到文件时



>输入:

 bank_details
 payee
 online_system_id
 transaction_id
 pass_num
 prev_tran_approved
 bank_details
 payee
 online_system_id
 transaction_id
 prev_tran_approved

以下代码的实际输出。

  bank_details
 payee
 online_system_id
 transaction_id
 pass_num
 prev_tran_approved
 online_system_id
 proc_online_system_id
 tran_reversed
 prev_tran_approved
 bank_details
 payee
 online_system_id
 transaction_id
 prev_tran_approved
 online_system_id
 proc_online_system_id
 tran_reversed
 prev_tran_approved

预期输出:

 bank_details
 payee
 online_system_id
 transaction_id
 pass_num
 prev_tran_approved
 proc_online_system_id
 tran_reversed
 bank_details
 payee
 online_system_id
 transaction_id
 prev_tran_approved
 proc_online_system_id
 tran_reversed

下面给出的代码仅供参考。我检查文件中现有字符串的 if 条件总是重新为 true,即使文件中不存在字符串。请浏览代码并建议我进行更改。

那么逻辑是我们必须读取一个包含各种行的文件,其中每条记录都分隔一个空白行。根据我在 if 块中提到的条件,即如果字符串online_system_id存在于文件中,我们不会附加它,如果它不存在,那么我们将它附加到每个记录块的末尾。此检查将针对 4 个值执行,即 online_system_id,proc_online_system_id,tran_reversed,prev_tran_approved

   String line;
    File output = new File(outputDir, "post_tran.tbl");
    PrintStream out = new PrintStream(new FileOutputStream(output));
    BufferedReader br = new BufferedReader(new FileReader(inputFile));
    while((line=br.readLine())!=null){
        if(line.trim().isEmpty()) { 
            if(!(line.contains("online_system_id"))){
                out.println("online_system_id");
            }
            if(!(line.contains("proc_online_system_id"))){
                out.println("proc_online_system_id");
            }    
            if(!(line.contains("tran_reversed"))){
                out.println("tran_reversed");
            }  
            if(!(line.contains("prev_tran_approved"))){
                out.println("prev_tran_approved");
            }  
        }
        out2.println(line);        //always print the current line
      }

      try{
        out2.close(); 
        br.close();
      } catch (Exception ex) {
        System.err.println(ex);
      }

编辑:

在解释了您的逻辑之后,看起来您需要跟踪当前记录中出现了 4 个特殊字符串中的哪一个,并在记录结束时添加其中任何一个没有出现。您可以使用 4 个布尔变量来做到这一点。

boolean osi = false;
boolean posi = false;
boolean tr = false;
boolean pta = false;
while((line=br.readLine())!=null){
    if(line.trim().isEmpty()) { 
        // end of record, print each of the 4 special Strings that didn't appear
        if(!osi){
            out.println("online_system_id");
        }
        if(!posi){
            out.println("proc_online_system_id");
        }    
        if(!tr){
            out.println("tran_reversed");
        }  
        if(!pta){
            out.println("prev_tran_approved");
        }
        osi = false;
        posi = false;
        tr = false;
        pta = false;  
    } else {
        // track appearance of the 4 special Strings
        if(line.contains("online_system_id")){
            osi = true;
        }
        if(line.contains("proc_online_system_id")){
            posi = true;
        }    
        if(line.contains("tran_reversed")){
            tr = true;
        }  
        if(line.contains("prev_tran_approved")){
            pta = true;
        }  
    }
    out.println(line);        //always print the current line
}

更改

if(line.trim().isEmpty()) 

if(!line.trim().isEmpty()) 

也:

 if((line.contains("online_system_id"))){
                out.println("online_system_id");
            }
            if((line.contains("proc_online_system_id"))){
                out.println("proc_online_system_id");
            }    
            if((line.contains("tran_reversed"))){
                out.println("tran_reversed");
            }  
            if((line.contains("prev_tran_approved"))){
                out.println("prev_tran_approved");
            }  

相关内容

  • 没有找到相关文章

最新更新