Groovy中createFolder方法中的异常

时间:2013-12-14 10:35:24

标签: groovy

运行以下代码时出现以下错误

groovy.lang.MissingMethodException: No signature of method: Script17.createFolder() is applicable for argument types: (java.lang.String, java.lang.String) values: [C:/, testFolder] error at line: 38

代码

def directory = 'C:/'
def folderName = 'testFolder'
def c

def txtFileInfo = []

String a = "Today is a new day"
String b = "Tomorrow is the future"
String d = "Yesterday is the past"

txtFileInfo << a
txtFileInfo << b
txtFileInfo << d

c = createFolder(directory, folderName) //this simply creates a folder to drop the txt file in

writeToFile(c, "garbage", ".txt", txtFileInfo)




public void writeToFile(def directory, def fileName, def extension, def infoList) {
    File file = new File("$directory/$fileName$extension")

    infoList.each {
        file << ("${it}\r\n")
    }
}

方法中的参数有问题,不应该都是字符串吗?

由于

1 个答案:

答案 0 :(得分:2)

您可以修改代码,使其如下所示:

def directory = './'
def folderName = 'testFolder'
def c

def txtFileInfo = []

txtFileInfo << "Today is a new day"
txtFileInfo << "Tomorrow is the future"
txtFileInfo << "Yesterday is the past"

c = createFolder(directory, folderName)

writeToFile(c, "garbage", ".txt", txtFileInfo)


def createFolder(directory, folderName) {
    new File(directory + folderName).mkdir() 
    directory + folderName
}

public void writeToFile(String directory, String fileName, String extension, List<String> infoList) {
    File file = new File("$directory/$fileName$extension")

    infoList.each {
        file << (it + System.getProperty("line.separator"))
    }
}

如果要重新使用代码,请将其放在一个类中,并将该类导入到您的文件中,确保在运行程序时该类位于类路径中。 Groovy documentation上记录了执行此操作的方法。

和平