标签:exception set return reader cti replace ++ ack cat
import com.univocity.parsers.tsv.TsvParser; import com.univocity.parsers.tsv.TsvParserSettings; import org.springframework.util.ClassUtils;
public class StringCompareUtil {
public static Object readTsv(String url) { System.out.println(new java.util.Date().toString()); List<Object> list = new ArrayList<Object>(); try { // 创建tsv解析器settings配置对象 TsvParserSettings settings = new TsvParserSettings(); settings.getFormat().setLineSeparator("\n"); TsvParser parser = new TsvParser(settings); DataInputStream in = new DataInputStream(new FileInputStream(new File(url))); BufferedReader br = new BufferedReader(new InputStreamReader(in, "GBK"));//这里如果csv文件编码格式是utf-8,改成utf-8即可 List<String[]> allRows = parser.parseAll(br); for (int i = 1; i < allRows.size(); i++) { //忽略第一行 Map<String, String> map = new HashMap<>(); map.put("LATITUDE", (allRows.get(i)[3].trim()).replaceAll("\"", "")); map.put("LONGITUDE", (allRows.get(i)[4].trim()).replaceAll("\"", "")); map.put("NAMEPATH", (allRows.get(i)[5].trim()).replaceAll("\"", "")); System.out.println(Arrays.asList(allRows.get(i))); list.add(map); } } catch (Exception e) { e.printStackTrace(); } return list; } }
<!--读取tsv文件--> <dependency> <groupId>com.univocity</groupId> <artifactId>univocity-parsers</artifactId> <version>2.6.1</version> <type>jar</type> </dependency>
标签:exception set return reader cti replace ++ ack cat
原文地址:https://www.cnblogs.com/tongsi/p/12737157.html