【发布时间】:2010-10-29 09:34:16
【问题描述】:
我对此很陌生,这可能是一个非常明显的答案。在我的 csv 脚本中,我试图弄清楚如何使用 CSVReader 类读取 csv 文件并写入新的 csv 文件。错误告诉我 writeNext(java.lang.String[]) 不能应用于 (java.lang.String)。我尝试使用直接分配和 getString() 方法,但没有任何效果。有任何想法吗?
我有以下代码: 更新
public static void main(String[] args) throws IOException {
//read csv file
CSVReader reader = new CSVReader(new FileReader(ADDRESS_FILE));
//write to csv file
CSVWriter writer2 = new CSVWriter(new FileWriter("output.csv"), '\t');
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
System.out.println("Name: [" + nextLine[0] + "]\nAddress: [" + nextLine[1] + "]\nEmail: [" + nextLine[2] + "]");
String[] newRow = new String[] {nextLine[0],nextLine[2], nextLine[1]};
}
//writer2.writeNext(entries);
writer2.writeAll(newRow);
writer2.close();
}
错误: c:\java\examples\ETHImport.java:46: 找不到符号 符号:变量newRow 位置: ETHImport 类 writer2.writeAll(newRow);
1 个错误
提前致谢。
【问题讨论】:
-
我又累又懒,懒得为你做所有事情:哪里出错了?
-
在您的新代码中,您只是缺少“新”。它应该是“String [] newRow = new String[] ...”
-
酷.. 这真的很有帮助.. 不知道为什么我会得到一个在 while 循环中声明的变量
-
查看我的回答中的评论 - 您需要在循环中使用 writer2.writeNext(newRow)。删除它外面的线。