违反指示,G4辅路,东向西,"118.266529560,39.869942703",999350003108,52,0,0,0,3,3,轨迹
需要达到的目的如下:
["违反指示","G4辅路","东向西","118.266529560,39.869942703","999350003108","52","0","0","0","3","3","轨迹"]
使用正则表达式配合.split()
进行字符串分割
使用cn.hutool.core.text.csv
中的CsvReader
和CsvData
进行分割
1. 使用正则表达式配合.split()
进行字符串分割
package com.data;
import org.junit.Test;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DataPushServiceTest {
@Test
public void testFun(){
String line = "违反指示,G4辅路,东向西,\"118.266529560,39.869942703\",999350003108,52,0,0,0,3,3,轨迹";
String[] res = line.trim().split(",(?=([^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)",-1);
for (String s : res){
System.out.println(s);
System.out.println(res);
output:
"118.266529560,39.869942703"
999350003108
违反指示,G4辅路,东向西,"118.266529560,39.869942703",999350003108,52,0,0,0,3,3,轨迹
2. 使用cn.hutool.core.text.csv
中的CsvReader
和CsvData
进行分割
package com.data;
import org.junit.Test;
import cn.hutool.core.text.csv.CsvData;
import cn.hutool.core.text.csv.CsvReader;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DataPushServiceTest {
@Test
public void testFun(){
CsvReader csvReader = new CsvReader();
File file = new File("E:/data/bj_weizhang.csv");
CsvData read = csvReader.read(file);
String coords = read.getRow(1).getByName("coords");
System.out.println(coords);
output:
"118.266529560,39.869942703"
使用这个方法更简单,但是该方法会一次加载文件的全部内容,文件较大是对性能要求较高,慎用!