java split用法 案例
需求:java读取一个csv文件并将文件内容每行按照","隔开
场景一:
读取1.csv文件:文件内容如下:
1,zhangsan,note
2,lisi,
注意:第二行逗号后面没有数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public static void main(String[] args) { String csvFile = "C:\\Users\\yc\\Desktop\\1.csv" ; String line; try (BufferedReader br = new BufferedReader( new FileReader(csvFile))) { // 读取CSV文件的每一行 while ((line = br.readLine()) != null ) { // 根据分隔符拆分行数据 String[] data = line.split( "," ); // 打印行数据 System.out.println( "-----------长度:" +data.length); for ( int i= 0 ;i<data.length;i++){ System.out.println(data[i]); } } } catch (IOException e) { e.printStackTrace(); } } |
输出结果:两行分割后数据长度不一样
如何让分割后的长度一样,避免下标溢出报错?修改代码,重点如下红色部分line.split(",",-1);加个-1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public static void main(String[] args) { String csvFile = "C:\\Users\\yc\\Desktop\\1.csv" ; String line; try (BufferedReader br = new BufferedReader( new FileReader(csvFile))) { // 读取CSV文件的每一行 while ((line = br.readLine()) != null ) { // 根据分隔符拆分行数据 String[] data = line.split( "," ,- 1 ); // 打印行数据 System.out.println( "-----------长度:" +data.length); for ( int i= 0 ;i<data.length;i++){ System.out.println(data[i]); } } } catch (IOException e) { e.printStackTrace(); } } |
输出结果:长度一样了,第二行输出了一个空,如下图所示。
场景二:
读取2.csv文件:文件内容如下:
aaa,"[123213,123123]",bbbb
想分割成aaa,"[123213,123123]"和bbbb
上代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public static void main(String[] args) { String csvFile = "C:\\Users\\yc\\Desktop\\2.csv" ; String line; try (BufferedReader br = new BufferedReader( new FileReader(csvFile))) { // 读取CSV文件的每一行 while ((line = br.readLine()) != null ) { // 根据分隔符拆分行数据 String[] data = line.split( "," ,- 1 ); // 打印行数据 System.out.println( "-----------长度:" +data.length); for ( int i= 0 ;i<data.length;i++){ System.out.println(data[i]); } } } catch (IOException e) { e.printStackTrace(); } } |
输出结果:事与愿违并没有出处想要的结果
修改代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | public static void main(String[] args) { String csvFile = "C:\\Users\\yc\\Desktop\\2.csv" ; String line; try (BufferedReader br = new BufferedReader( new FileReader(csvFile))) { // 读取CSV文件的每一行 while ((line = br.readLine()) != null ) { // 根据分隔符拆分行数据 List<String> data = splitString(line); // 打印行数据 System.out.println( "-----------长度:" +data.size()); for ( int i= 0 ;i<data.size();i++){ System.out.println(data.get(i)); } } } catch (IOException e) { e.printStackTrace(); } } public static List<String> splitString(String text) { List<String> result = new ArrayList<>(); boolean inQuotes = false ; StringBuilder sb = new StringBuilder(); for ( char c : text.toCharArray()) { if (c == '\"' ) { inQuotes = !inQuotes; sb.append(c); } else if (c == ',' && !inQuotes) { result.add(sb.toString()); sb.setLength( 0 ); } else { sb.append(c); } } result.add(sb.toString()); return result; } |
输出结果:得到了想要的结果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
2021-05-14 kafka搭建二、集群搭建