1 回答
TA贡献2041条经验 获得超4个赞
无论我的需要如何,我总是创建相同的方法,然后调整输入
public static void printAtFile(String filename, String header, String content[])
{
System.out.println("Start creating file "+filename);
PrintWriter writer = null;
try {
writer = new PrintWriter(filename);
writer.println(header);
for(String u:content)
writer.println(u);
writer.close();
} catch (Exception ex) {
System.out.println("Error while writing at file "+filename);
}
}
在这种情况下,你想做类似的事情
String h = "Column1,column2"
String content[] = new String[N];
Arrays.fill(content, "");
for(int i=0;i<N;i++)
{
if(...)content[i]="whateveryouwant,"+"blank";
else content[i]="blank,"+"whateveryouwant";
}
printAtFile("file.csv",h,content);
添加回答
举报