Groovy 如何解决除''\n'';;不管怎样,我明白了

Groovy 如何解决除''\n'';;不管怎样,我明白了,groovy,Groovy,我的代码中有以下行 def genList = (args[]?.size() >=4)?args[3]: " 当我运行我的全部代码时,我得到以下错误 需要除“”以外的任何内容\n“”;在第9行第113列找到了 在这里,我添加了整个代码,以便您可以看到我在做什么 def copyAndReplaceText(source, dest, targetText, replaceText){ dest.write(source.text.replaceAll(targetText, r

我的代码中有以下行

def genList = (args[]?.size() >=4)?args[3]: "
当我运行我的全部代码时,我得到以下错误

需要除“”以外的任何内容\n“”;在第9行第113列找到了

在这里,我添加了整个代码,以便您可以看到我在做什么

def copyAndReplaceText(source, dest, targetText, replaceText){
    dest.write(source.text.replaceAll(targetText, replaceText))
}
def dire = new File(args[0])
def genList = (args[]?.size() >=4)?args[3]: " // check here if argument 4 is provided, and generate output if so
def outputList = ""
dire.eachFile { 
    if (it.isFile()) {
        println it.canonicalPath
        // TODO 1: copy source file to *.bak file
        copy = { File src,File dest-> 

            def input = src.newDataInputStream()
            def output = dest.newDataOutputStream()

            output << input 

            input.close()
            output.close()
        }

        //File srcFile  = new File(args[0])
        //File destFile = new File(args[1])

        //File srcFile  = new File('/geretd/resume.txt')
        //File destFile = new File('/geretd/resumebak.txt')
        File srcFile = it
        File destFile = newFile(srcFile + '~')
        copy(srcFile,destFile)

        // search and replace to temporary file named xxxx~, old text with new text. TODO 2: modify copyAndReplaceText to take 4 parameters.
        if( copyAndReplaceText(it, it+"~",   args[1], args[2]) ) {
   // TODO 3: remove old file (it)
               it.delete()
   // TODO 4: rename temporary file (it+"~") to (it)

   // If file was modified and parameter 4 was provided, add modified file name (it) to list
   if (genList != null) { 
     // add modified name to list
     outputList += it + "\n\r"
   }
  }
    }
}
// TODO 5: if outputList is not empty (""), generate to required file (args[3])
if (outputList != ""){
    def outPut = new File(genList)
    outPut.write(outputList)
 } 
def copyAndReplaceText(源、目标、目标文本、替换文本){
dest.write(source.text.replaceAll(targetText,replaceText))
}
def dire=新文件(参数[0])
def genList=(args[]?.size()>=4)?args[3]:“//检查此处是否提供了参数4,如果提供了,则生成输出
def outputList=“”
dire.eachFile{
if(it.isFile()){
println it.canonicalPath
//TODO 1:将源文件复制到*.bak文件
复制={File src,File dest->
def input=src.newDataInputStream()
def output=dest.newDataOutputStream()

输出为什么在此行末尾有一个
def genList=(args[]?.size()>=4)?args[3]:“


您需要这样做:
def genList=(args[]?.size()>=4)?args[3]:“

您需要在
def outputList=”“
的末尾添加一个
标记。”


同时去掉
def genList=(args[]?.size()>=4)末尾的
”:args[3]:“

只需关闭双引号

def genList = (args?.size() >=4)?args[3]: ""

我对
进行了注释,现在我得到了以下内容:第10行第1列的意外标记:def,它将与
def outputList=”“
相关。@geretd刚刚注释掉了双引号,因此代码没有以前那么有意义。而不是注释掉
用另一个来关闭它,即:
。即使上面的github代码高亮显示了错误的位置,我还是按照你们的建议做了,我仍然得到了错误,我不知道是什么错了……还有什么想法,关于我能做些什么来修复它?哦,天哪,我犯了一个愚蠢的错误。是的,只需添加另一个
并将其设置为
def-genList=(args[]?.size()>=4)?args[3]:”“
!@lapint是的,这就是我在这里所做的事情。了解def-genList=(args[]?.size()>=4)?args[3]:”def outputList=“”谢谢我照你说的做了,但我还是遇到了一个例外:
第10行第1列的意外标记:def
有什么想法吗?非常感谢much@dmahapatro你能告诉我它应该是什么样子吗?这就是它现在的样子:
def genList=(args[]?.size()>=4)?args[3]:“”
我得到了这个错误:
java.lang.ArrayIndexOutOfBoundsException:0位于org.codehaus.groovy.runtime.dgmimpl.arrays.ObjectArrayGetAtMetaMethod.invoke(ObjectArrayGetAtMetaMethod.java:38)的groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
@geretd简而言之,你到底想从这个脚本中获得什么?我做到了,现在我得到了这个错误,(如果我问得太多,很抱歉,我只有两天的时间来学习这个对我来说是新的语言)
java.lang.ArrayIndexOutOfBoundsException:0位于org.codehaus.groovy.runtime.dgmimpl.arrays.ObjectArrayGetAtMetaMethod.invoke(ObjectArrayGetAtMetaMethod.java:38)的groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
尝试从
args[]?.size()中删除
[/code>