假设CSV文件格式如下:
ID, 科目,成绩
201, 2, 90
201,3, 100
206, 2, 85
206, 3, 78第一行是标题, 现在需要写一个方法,这个方法的参数是ID,功能是把指定的ID及相关内容复制到另一个CSV文件。
例如指定ID为201,则复制到目标CSV后,内容如下:ID, 科目,成绩
201, 2, 90
201,3, 100
ID, 科目,成绩
201, 2, 90
201,3, 100
206, 2, 85
206, 3, 78第一行是标题, 现在需要写一个方法,这个方法的参数是ID,功能是把指定的ID及相关内容复制到另一个CSV文件。
例如指定ID为201,则复制到目标CSV后,内容如下:ID, 科目,成绩
201, 2, 90
201,3, 100
String srcFile = "src.csv";
String dstFile = "dst.csv";
String id = "201";
BufferedReader br = new BufferedReader(new FileReader(srcFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(dstFile));
String line = br.readLine();
bw.write(line);bw.newLine();
while((line=br.readLine())!=null){
String str[] = line.split("\\,|\\,");
if(id.equals(str[0].trim())){
bw.write(line);bw.newLine();bw.flush();
}
}
br.close();
bw.close();