Groovy 不在函数内打印字符串
Groovy doesn't print the string inside a function
我是 groovy 的新手。我有以下代码,我想在控制台上打印字符串,但它不起作用:
import java.io.FileWriter;
import java.util.Arrays;
import java.io.Writer;
import java.util.List;
//Default separator
char SEPARATOR = ',';
//get path of csv file (creates new one if its not exists)
String csvFile = "c:";
println "========================= csvFile";
println csvFile;
String[] params = {"hello"};
writeLine(params, SEPARATOR);
//function write line in csv
public void writeLine(String[] params, char separator)
{
boolean firstParam = true;
println params;
StringBuilder stringBuilder = new StringBuilder();
String param = "";
for (int i = 0; i < params.length; i++)
{
//get param
param = params[i];
println param;
//if the first param in the line, separator is not needed
if (!firstParam)
{
stringBuilder.append(separator);
}
//Add param to line
stringBuilder.append(param);
firstParam = false;
}
//prepare file to next line
stringBuilder.append("\n");
//add to file the line
println stringBuilder.toString();
}
它给出以下输出:
在 groovy 中声明数组你必须使用方括号:
String[] params = ["hello"]
顺便说一句,整个代码可以简化为:
String[] params = ["hello"]
def writeLine(params, separator=','){
println params.join(separator)
}
writeLine(params)
我是 groovy 的新手。我有以下代码,我想在控制台上打印字符串,但它不起作用:
import java.io.FileWriter;
import java.util.Arrays;
import java.io.Writer;
import java.util.List;
//Default separator
char SEPARATOR = ',';
//get path of csv file (creates new one if its not exists)
String csvFile = "c:";
println "========================= csvFile";
println csvFile;
String[] params = {"hello"};
writeLine(params, SEPARATOR);
//function write line in csv
public void writeLine(String[] params, char separator)
{
boolean firstParam = true;
println params;
StringBuilder stringBuilder = new StringBuilder();
String param = "";
for (int i = 0; i < params.length; i++)
{
//get param
param = params[i];
println param;
//if the first param in the line, separator is not needed
if (!firstParam)
{
stringBuilder.append(separator);
}
//Add param to line
stringBuilder.append(param);
firstParam = false;
}
//prepare file to next line
stringBuilder.append("\n");
//add to file the line
println stringBuilder.toString();
}
它给出以下输出:
在 groovy 中声明数组你必须使用方括号:
String[] params = ["hello"]
顺便说一句,整个代码可以简化为:
String[] params = ["hello"]
def writeLine(params, separator=','){
println params.join(separator)
}
writeLine(params)