在读csv文件时,读完后,原本一个个单元格中的数据,全都都放在了第一个单元格中。。
怎么读取不会改变csv文件的原来格式(也就是说数据还是放在各自的单元格中) public static void main(String[] args) {
String path = "C:\\file\\aaa.csv";
String value = "";
try {
File csv = new File(path); BufferedReader br = new BufferedReader(new FileReader(csv));
while (br.ready()) {
String line = br.readLine();
StringTokenizer st = new StringTokenizer(line, ",\\s");
while (st.hasMoreElements()) {
String next = st.nextToken();
if (" ".equals(next)) {
System.out.print("null\t");
} else {
System.out.print(next + "\t");
}
}
System.out.println();
}
br.close(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
想要的结果比如文件中
怎么读取不会改变csv文件的原来格式(也就是说数据还是放在各自的单元格中) public static void main(String[] args) {
String path = "C:\\file\\aaa.csv";
String value = "";
try {
File csv = new File(path); BufferedReader br = new BufferedReader(new FileReader(csv));
while (br.ready()) {
String line = br.readLine();
StringTokenizer st = new StringTokenizer(line, ",\\s");
while (st.hasMoreElements()) {
String next = st.nextToken();
if (" ".equals(next)) {
System.out.print("null\t");
} else {
System.out.print(next + "\t");
}
}
System.out.println();
}
br.close(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
想要的结果比如文件中
原本的csv格式:
ABCD为列;1234为行号
A B C D
1 a1 b1 c1 d1
2 a2
3 a3 c3
4 b4 d4
运行以上代码结果:
a1 b1 c1 d1
a2
a3 c3
d4 d4
但不知道什么时候,原来文件中的格式变成
A B C D
1 a1b1c1d1
2 a2
3 a3c3
4 b4d4
这样了。。
然后读出来也变得有双引号了..
csv一直没变
FileWriter fw = new FileWriter("E:\\helloCsv.csv");
String str = ""+rs.getString(1)+",+"+rs.getString(2)+","+rs.getString(3)+","+rs.getString(4)+","+rs.getString(5);
fw.write(str);
fw.close();
我觉得java2000_net说的应该好用