使用Katalon Studio的Groovy脚本来实现自动化比较两个文件夹下同名的csv文件是否一致,并将比较结果输出到另一个文件中。要求文件一致时报文件一致的log,不一致时定位到哪个文件的哪一行数据不一致
时间: 2023-12-03 15:42:43 浏览: 134
以下是使用Katalon Studio的Groovy脚本实现自动化比较两个文件夹下同名的csv文件是否一致,并将比较结果输出到另一个文件中的示例代码:
```groovy
import com.opencsv.CSVReader
import com.opencsv.CSVReaderBuilder
import com.opencsv.exceptions.CsvException
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
// 定义两个文件夹路径
def folder1Path = Paths.get("path/to/folder1")
def folder2Path = Paths.get("path/to/folder2")
// 定义输出文件路径
def outputPath = Paths.get("path/to/output.csv")
// 遍历文件夹1中的所有csv文件
Files.newDirectoryStream(folder1Path, "*.csv").each { file1Path ->
// 获取文件2的路径
def file2Path = folder2Path.resolve(file1Path.fileName)
// 如果文件2不存在,则报错并退出
if (!Files.exists(file2Path)) {
throw new RuntimeException("File ${file2Path} does not exist")
}
// 比较两个文件
compareFiles(file1Path, file2Path, outputPath)
}
/**
* 比较两个csv文件
* @param file1Path 文件1路径
* @param file2Path 文件2路径
* @param outputPath 输出文件路径
*/
def compareFiles(Path file1Path, Path file2Path, Path outputPath) {
// 读取文件1的数据
def file1Data = readCsvFile(file1Path)
// 读取文件2的数据
def file2Data = readCsvFile(file2Path)
// 如果行数不一致,则输出到输出文件中
if (file1Data.size() != file2Data.size()) {
writeOutput(outputPath, "${file1Path} and ${file2Path} have different number of rows")
return
}
// 比较每一行数据
for (int i = 0; i < file1Data.size(); i++) {
def file1Row = file1Data.get(i)
def file2Row = file2Data.get(i)
// 如果列数不一致,则输出到输出文件中
if (file1Row.size() != file2Row.size()) {
writeOutput(outputPath, "Row ${i + 1} in ${file1Path} and ${file2Path} have different number of columns")
continue
}
// 比较每一列数据
for (int j = 0; j < file1Row.size(); j++) {
if (!file1Row.get(j).equals(file2Row.get(j))) {
writeOutput(outputPath, "Row ${i + 1} and column ${j + 1} in ${file1Path} and ${file2Path} have different values: ${file1Row.get(j)} != ${file2Row.get(j)}")
}
}
}
// 如果两个文件一致,则输出到输出文件中
writeOutput(outputPath, "${file1Path} and ${file2Path} are identical")
}
/**
* 读取csv文件
* @param filePath 文件路径
* @return 读取的数据
*/
def readCsvFile(Path filePath) {
def reader = new CSVReaderBuilder(new FileReader(filePath.toFile())).withSkipLines(1).build()
def data = []
try {
reader.forEach { line ->
data.add(line.toList())
}
} catch (CsvException e) {
throw new RuntimeException("Failed to read ${filePath}: ${e.getMessage()}", e)
}
return data
}
/**
* 输出比较结果到输出文件中
* @param outputPath 输出文件路径
* @param message 比较结果消息
*/
def writeOutput(Path outputPath, String message) {
Files.write(outputPath, "${message}\n".getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND)
}
```
该脚本会遍历文件夹1中的所有csv文件,对于每个文件,会判断其是否存在对应的同名文件2,如果不存在则报错并退出。然后会比较两个文件的数据,如果行数或者列数不一致,则输出到输出文件中,如果某一行某一列的数据不一致,则输出到输出文件中,最后如果两个文件一致,则输出到输出文件中。比较结果都会以行的形式输出到输出文件中。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)