Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groovy—在文件中间添加一行而不修改新行字符_Groovy - Fatal编程技术网

Groovy—在文件中间添加一行而不修改新行字符

Groovy—在文件中间添加一行而不修改新行字符,groovy,Groovy,我正在使用下面的groovy脚本对某些文件集(比如Java文件)进行更改,在这里,如果文件中还没有包,我想在文件中添加包,下面的代码可以完成这项工作(这里可以随意建议优化),但它会更新文件中的换行符,这是我宁愿避免的,有什么建议吗 void addIfRequired(def directory, def filePath) { def inputFile = new File(filePath) boolean containsPackage = false; List

我正在使用下面的groovy脚本对某些文件集(比如Java文件)进行更改,在这里,如果文件中还没有包,我想在文件中添加包,下面的代码可以完成这项工作(这里可以随意建议优化),但它会更新文件中的换行符,这是我宁愿避免的,有什么建议吗

void addIfRequired(def directory, def filePath) {
    def inputFile = new File(filePath)
    boolean containsPackage = false;
    List<String> lines = inputFile.readLines();
    for (def eachLine : lines) {
        eachLine = eachLine.trim();
        if (eachLine.startsWith("package ")) {
            containsPackage = true
            break
        } else if (eachLine.startsWith("import ")) {
            // Stop looking if we don't get package till import statement.
            break;
        }
    }
    if (!containsPackage) {
        String lineSeparator = System.getProperty("line.separator");
        // The following API generates the package name
        def packageN = getPackage(inputFile, directory)
        boolean packageAdded = false;
        StringBuilder outputTxt = new StringBuilder();
        for (def line : lines) {
            if (!packageAdded && line.startsWith("import ")) {
                outputTxt.append("package " + packageN + ";" + lineSeparator)
                packageAdded = true;
            }
            outputTxt.append(line);
            outputTxt.append(lineSeparator);
        }
        inputFile.write(outputTxt.toString());
    }
}
void addirequired(def目录,def文件路径){
def inputFile=新文件(文件路径)
布尔containsPackage=false;
列表行=inputFile.readLines();
对于(def eachLine:行){
eachLine=eachLine.trim();
if(eachLine.startsWith(“包”)){
containsPackage=true
打破
}else if(eachLine.startsWith(“导入”)){
//如果我们在进口声明之前没有收到包裹,请停止查看。
打破
}
}
如果(!containsPackage){
字符串lineSeparator=System.getProperty(“line.separator”);
//下面的API生成包名
def packageN=getPackage(输入文件,目录)
布尔值packageAdded=false;
StringBuilder OutputText=新的StringBuilder();
用于(def管路:管路){
如果(!packageAdded&&line.startsWith(“导入”)){
outputText.append(“package”+packageN+”;“+lineSeparator)
packageAdded=true;
}
outputText.append(行);
outputText.append(行分隔符);
}
write(outputText.toString());
}
}

我相信您的解决方案可以处理这种情况,但请记住,对于输入文件,例如:

// Dangerous comment
// 
// import com.oldcompany.Bogus;

import com.foo.Example;

public MyClass {
}
但是,如果没有导入,您(和我的)的解决方案都将失败:

// random comment
// 

public MyClass {
}
假设必须有导入,下面是一种方法,使用正则表达式:

def addIfRequired = { def directory, def filePath ->
    def inputFile = new File(filePath)
    boolean containsPackage = false;
    List<String> lines = inputFile.readLines();
    for (def eachLine : lines) {
        eachLine = eachLine.trim();
        if (eachLine.startsWith("package ")) {
            containsPackage = true
            break
        } else if (eachLine.startsWith("import ")) {
            // Stop looking if we don't get package till import statement.
            break;
        }
    }
    if (!containsPackage) {
        def lineSeparator = System.getProperty("line.separator")
        // The following API generates the package name
        // def packageN = getPackage(inputFile, directory)
        def packageN = lineSeparator + "package com.abc.example;" + lineSeparator
        // TODO: potentially re-write the above code to use getText() so that 
        // we aren't reading the file twice. This could be tricky if the file is 
        // from an OS different from the OS used for this script ?

        String text = inputFile.getText()
        String replacement = packageN + "${lineSeparator}import"
        def regex = lineSeparator + /\s*import/
        String newText = text.replaceFirst(regex, replacement)

        inputFile.withWriter { def writer ->
            writer.write(newText)
        }        
    }
}
def addirequired={def目录,def文件路径->
def inputFile=新文件(文件路径)
布尔containsPackage=false;
列表行=inputFile.readLines();
对于(def eachLine:行){
eachLine=eachLine.trim();
if(eachLine.startsWith(“包”)){
containsPackage=true
打破
}else if(eachLine.startsWith(“导入”)){
//如果我们在进口声明之前没有收到包裹,请停止查看。
打破
}
}
如果(!containsPackage){
def lineSeparator=System.getProperty(“line.separator”)
//下面的API生成包名
//def packageN=getPackage(输入文件,目录)
def packageN=lineSeparator+“package com.abc.example;”+lineSeparator
//TODO:可能重新编写上述代码以使用getText(),以便
//我们没有读取该文件两次。如果该文件
//来自与此脚本所用操作系统不同的操作系统?
String text=inputFile.getText()
字符串替换=packageN+“${lineSeparator}导入”
def regex=lineSeparator+/\s*导入/
String newText=text.replaceFirst(regex,replacement)
inputFile.withWriter{def writer->
writer.write(新文本)
}        
}
}
注:

  • 谓词代码(用于确定包是否存在)不变。缺点是它会读取文件两次。重写谓词代码留给读者作为练习
  • 这段代码从import语句中删除前导空格。这有点混乱,但我认为可以纠正,如果这是一个优先事项的话
  • 为了纠正“无导入”问题,我们可能只在第一行编写包

这些文件是在运行脚本的操作系统之外的其他操作系统上创建的吗?行分隔符的系统属性是您在每行之后添加的,它可以更改中存在的原始行分隔符file@kunal我想
lineSeparator
应该独立于操作系统。根据您正在使用的系统进行拾取,对吗?此外,OP正在从上到下重写整个文件。@dmahapatro是的,如果每一行都以当前操作系统行结尾重写,这可能与写入这些文件的操作系统不同。在这种情况下,每一行都会更新,对吗?可能是OP所关心的。@kunal你说得对。我看了同样的东西,仔细阅读了你的评论。:-)