1

结果树的屏幕截图

代码一直在无限循环中运行,无论数据是否匹配。我也想在比较时忽略第一列/标题列。你能
帮忙吗?谢谢

我正在使用下面的代码来比较 2 个 csv 文件。

输入文件:

文件 1

ColumnA
A
B
C

文件2

ColumnA
A
B
D

代码

#import java.io.File;#
#import java.io.FileReader;#
#import java.io.LineNumberReader;#

String strfile1 = vars.get("thisScriptPath") + "file1.csv";
String strfile2 = vars.get("thisScriptPath") + "file2.csv";

def file1 = new File('/path/to/file1')
def file2 = new File('/path/to/file2')

def file1Lines = file1.readLines('UTF-8')
def file2Lines = file2.readLines('UTF-8')

if (file1Lines.size() != file2Lines.size()) {
    SampleResult.setSussessful(false)
    SampleResult.setResponseMessage('Files size is different, omitting line-by-line compare')
} else {
    def differences = new StringBuilder()

   file1Lines.eachWithIndex { String file1Line, int number ->
       String file2Line = file2Lines.get(number)
       if (!file1Line.equals(file2Line)) {
           differences.append('Difference # ').append(number).append('. Expected:        ').append(file1Line).append('. Actual: ' + file2Line)
           differences.append(System.getProperty('line.separator'))
       }
    }

    if (differences.toString().length() > 0) {
        SampleResult.setSuccessful(false)
        SampleResult.setResponseMessage(differences.toString())
    }
}

参考图片

4

0 回答 0