csv看起来像
state name
IL Mike
Sam
CA Kate
Bill
Leah
应该如下
state name
IL Mike
IL Sam
CA Kate
CA Bill
CA Leah
我正在尝试如上图所示进行打印
PrintWriter pw1 = new PrintWriter("C:\\Users\\file.csv");
File file1 = new File("C:\\Users\\Book1.csv");
List<String> lines1 = Files.readAllLines(file1.toPath(), StandardCharsets.UTF_8);
for (String line : lines1) {
String[] array = line.split(",");
pw1.println(array[0]+","+array[1]);
}
pw1.flush();
pw1.close();
我怎样才能做到这一点
如果我了解您要做什么,请保存以前的状态,直到更改为止
我认为这是做到这一点的方法。
String state = "";
int counter =0;
for (String line : lines1) {
String[] array = line.split(",");
if (array[0] != null && !array[0].isEmpty()){
state = array[0];
counter = 0;
}
else{
counter++;
}
pw1.println(state+ counter +","+array[1]);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句