1 回答

TA貢獻2041條經驗 獲得超4個贊
無論我的需要如何,我總是創(chuàng)建相同的方法,然后調整輸入
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);
添加回答
舉報