如何在groovy(Jenkins)中从两个文件中编写一个文件?
我有两个*.hex文件(可以像ASCII文件一样处理)是从Jenkinsfile groovy管道构建脚本生成的。 成功生成后,作业工作区中有以下文件:如何在groovy(Jenkins)中从两个文件中编写一个文件?,groovy,jenkins-groovy,Groovy,Jenkins Groovy,我有两个*.hex文件(可以像ASCII文件一样处理)是从Jenkinsfile groovy管道构建脚本生成的。 成功生成后,作业工作区中有以下文件: a、 六角形 b、 六角形 我需要的是c.hex,它只是将两个a.hex和b.hex粘在一个文件中 在C中,它如下所示: static void mergeIntelHexFiles(const char* aFile, const char* bFile, const char* mergedFile) { FILE* readFileA
static void mergeIntelHexFiles(const char* aFile, const char* bFile, const char* mergedFile)
{
FILE* readFileA = fopen(aFile, "r");
FILE* readFileB = fopen(bFile, "r");
if ( (NULL != readFileA )
&& (NULL != readFileB ))
{
FILE* writeFile = fopen(mergedFile, "w");
if (NULL != writeFile)
{
char lineBuffer[MAX_PATH_LEN];
while (NULL != fgets(lineBuffer, MAX_PATH_LEN, readFileA ))
{
if (NULL == strstr(lineBuffer, ":00000001FF"))
{ // it is not the last line of the file
fputs(lineBuffer, writeFile); // copy the line without modification
}
}
while (NULL != fgets(lineBuffer, MAX_PATH_LEN, readFileB ))
{
fputs(lineBuffer, writeFile); // copy the line without modification
}
fclose(writeFile);
}
fclose(readFileA);
fclose(readFileB);
}
}
这仅将a.hex的每一行复制到c.hex,然后将b.hex的每一行复制到c.hex
但是在Jenkins Groovy我不知道怎么做
我只能找到简单示例的文档,如
所以问题是如何在groovy中将两个文件合并为一个?groovy在File类上有一些很好的重载方法,以及重载'
new File('c.hex')。text=new File('a.hex')。text+new File('b.hex')).text
假设它们只是ASCII文本,您还知道如何删除.hex的最后一行,然后合并文件吗?
new File("c.hex") << new File("a.hex").text << new File("b.hex").text
def cHex = new File("c.hex")
def lines = new File("a.hex").readLines()
lines.eachWithIndex { String line, int idx -> if(idx < lines.size() - 1) cHex << line << '\n' }
cHex << new File("b.hex").text