POM
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example.DemoCrud</groupId> <artifactId>democurd</artifactId> <version>0.0.1-SNAPSHOT</version> <name>democurd</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--csv文件操作--> <dependency> <groupId>net.sourceforge.javacsv</groupId> <artifactId>javacsv</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- mybatis整合Springboot --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> <dependency> <!-- maven 添加json--> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> </dependency> <!-- 引入org.json所需依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.70</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.3.0</version> </dependency> <!--bootstrap-webjar--> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>4.0.0</version> </dependency> <!--配置文件注入时使用后会有提示--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/oracle/oracle-jdbc --> <dependency> <groupId>com.oracle.ojdbc</groupId> <artifactId>ojdbc8</artifactId> <version>19.3.0.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <!-- Druid 数据源依赖,集成了 Spring boot ,方便配置 druid 属性 --> <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.8</version> </dependency> <!-- Mysql 数据库驱动,spring boot 2.1.3 时,mysql 驱动版本为 8.0.15 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- 引入 Spring Data JPA,JPA 组件内部会依赖 JDBC 组件,JDBC 组件内部默认依赖的是 HikariCP 数据源--> <!--切换 druid 数据源时,HikariCP 依赖可以移除掉,不移除时也不影响--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!--引用 log4j2 spring boot 启动器,内部依赖了 slf4j、log4j;排除项目中的 logback--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
DEMO
package com.example.democrud.democurd.Prototype.demo01; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.csvreader.CsvReader; import java.io.*; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; public class csvDemo { public static void main(String[] args) throws IOException { String filePath="C:\\Users\\闫文超\\Desktop\\啊工作表.csv"; //方式一 Students(); System.out.println("---------------- //方式一结尾-----------------------"); // //方式一 Studentss(); System.out.println("---------------- //方式二结尾-----------------------"); readCsvByCsvReader(filePath); System.out.println("---------------- //方式三结尾-----------------------"); System.out.println(readCsvByBufferedReader(filePath)); System.out.println("---------------- //方式四结尾-----------------------"); readCsvFile(filePath); System.out.println("---------------- //方式五结尾-----------------------"); } public static void readCsvFile(String filePath){ try { ArrayList<String[]> csvList = new ArrayList<String[]>(); CsvReader reader = new CsvReader(filePath,',',Charset.forName("GBK")); // reader.readHeaders(); //跳过表头,不跳可以注释掉 while(reader.readRecord()){ csvList.add(reader.getValues()); //按行读取,并把每一行的数据添加到list集合 } reader.close(); System.out.println("读取的行数:"+csvList.size()); for(int row=0;row<csvList.size();row++){ //打印每一行的数据 System.out.print(csvList.get(row)[0]+","); System.out.print(csvList.get(row)[1]+","); // System.out.print(csvList.get(row)[2]+","); // System.out.println(csvList.get(row)[3]+","); //如果第一列(即姓名列)包含lisa,则打印出lisa的年龄 if(csvList.get(row)[0].equals("lisa")){ System.out.println("lisa的年龄为:"+csvList.get(row)[2]); } } System.out.println("\r\n"); } catch (Exception e) { e.printStackTrace(); } } /** * BufferedReader 读取 * @param filePath * @return */ public static ArrayList<String> readCsvByBufferedReader(String filePath) { File csv = new File(filePath); csv.setReadable(true); csv.setWritable(true); InputStreamReader isr = null; BufferedReader br = null; try { isr = new InputStreamReader(new FileInputStream(csv), "gbk"); br = new BufferedReader(isr); } catch (Exception e) { e.printStackTrace(); } String line = ""; ArrayList<String> records = new ArrayList<>(); try { while ((line = br.readLine()) != null) { System.out.println(line); records.add(line); } System.out.println("csv表格读取行数:" + records.size()); } catch (IOException e) { e.printStackTrace(); } return records; } public static ArrayList<String> readCsvByCsvReader(String filePath) { ArrayList<String> strList = null; try { ArrayList<String[]> arrList = new ArrayList<String[]>(); strList = new ArrayList<String>(); //utf-8乱码 // CsvReader reader = new CsvReader(filePath, ',', Charset.forName("UTF-8")); CsvReader reader = new CsvReader(filePath, ',', Charset.forName("gbk")); while (reader.readRecord()) { // System.out.println(Arrays.asList(reader.getValues())); arrList.add(reader.getValues()); // 按行读取,并把每一行的数据添加到list集合 } reader.close(); System.out.println("--------"); System.out.println("读取的行数:" + arrList.size()); // 如果要返回 String[] 类型的 list 集合,则直接返回 arrList // 以下步骤是把 String[] 类型的 list 集合转化为 String 类型的 list 集合 for (int row = 0; row < arrList.size(); row++) { // 组装String字符串 // 如果不知道有多少列,则可再加一个循环 // String ele = arrList.get(row)[0] + "," + arrList.get(row)[1] + ","; // + arrList.get(row)[2] + "," + arrList.get(row)[3] ; String ele; for (int i = 0; i < arrList.size()-1; i++) { ele= arrList.get(row)[i]; strList.add(ele); System.out.println(ele); } } } catch (Exception e) { e.printStackTrace(); } return strList; } public static void Studentss() { try { BufferedReader reader =new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\闫文超\\Desktop\\啊工作表.csv"),"gbk")); reader.readLine();//第一行信息,为标题信息,不用,如果需要,注释掉 String line = null; while((line=reader.readLine())!=null){ String item[] = line.split(",");//CSV格式文件为逗号分隔符文件,这里根据逗号切分 String last = item[item.length-1];//这就是你要的数据了 //int value = Integer.parseInt(last);//如果是数值,可以转化为数值 System.out.println(last); } } catch (Exception e) { e.printStackTrace(); } } public static void Students() { File csv = new File("C:\\Users\\闫文超\\Desktop\\啊工作表.csv"); // CSV文件路径 BufferedReader br = null; try { //br = new BufferedReader(new FileReader(csv)); br = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\闫文超\\Desktop\\啊工作表.csv"), "gbk")); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } String line = ""; String everyLine = ""; try { List<String> allString = new ArrayList<>(); while ((line = br.readLine()) != null) //读取到的内容给line变量 { everyLine = line; System.out.println(everyLine); allString.add(everyLine); } System.out.println("csv表格中所有行数:" + allString.size()); } catch (IOException e) { e.printStackTrace(); } } }
测试结果
Name,age 谚文超,122 谚文超,68 csv表格中所有行数:3 ---------------- //方式一结尾----------------------- 122 68 ---------------- //方式二结尾----------------------- -------- 读取的行数:3 Name age 谚文超 122 谚文超 68 ---------------- //方式三结尾----------------------- Name,age 谚文超,122 谚文超,68 csv表格读取行数:3 [Name,age, 谚文超,122, 谚文超,68] ---------------- //方式四结尾----------------------- 读取的行数:3 Name,age,谚文超,122,谚文超,68, ---------------- //方式五结尾-----------------------
DEMO1
package com.example.democrud.democurd.Prototype.demo01; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.util.*; import java.util.stream.Collectors; public class ListAndMap1Demo { public static void main(String[] args) { // 循环中获取数据 stduentgetName(); //处理异常嵌套数据 stduents(); } public static void stduentgetName() { Map<String, List<Student>> map = stduentInit(); //System.out.println(JSONObject.toJSON(map)); for (Map.Entry<String, List<Student>> entry : map.entrySet()) { String key = entry.getKey(); List<Student> list = entry.getValue(); System.out.println("key===>"+key); System.out.println("list===>"+list); List<String> collect = list.stream().map(Student::getName).collect(Collectors.toList()); System.out.println("list: 集合数组==》" + collect); String collect1 = list.stream().map(Student::getName).collect(Collectors.joining(",")); System.out.println("string: String数组==》" + collect1); System.out.println("--------------------------------------1---------------------------------------------"); List<Student> collectle = list.stream().filter(v -> v.getAge() >= 20).collect(Collectors.toList()); System.out.println("collectle==>"+collectle); System.out.println("---------------------------------2--------------------------------------------------"); List<String> strList = new ArrayList<>(); for (Student stu : list) { strList.add(stu.getName()); } System.out.println("strList: " + strList); } } /** * 处理深层嵌套异常数据 */ public static void stduents() { List<Map<String, String>> list = new ArrayList<>(); String str = "{\"sheets\":[{\"datas\":[{\"cells\":[{\"dataType\":2,\"value\":\"闫文超\"},{\"dataType\":1,\"value\":\"10.0\"}]},{\"cells\":[{\"dataType\":2,\"value\":\"老王\"},{\"dataType\":1,\"value\":\"20.0\"}]}],\"name\":\"Sheet1\",\"titles\":[\"Name\",\"AGE\"]}]}"; JSONObject js = JSONObject.parseObject(str); //System.out.println(js); String sheets = js.get("sheets").toString(); //System.out.println(sheets); JSONArray objects = JSONArray.parseArray(sheets); //System.out.println(objects); for (Object object : objects) { JSONObject js111 = JSONObject.parseObject(object.toString()); //System.out.println(js111); String datas = js111.get("datas").toString(); //System.out.println(datas); JSONArray objects1 = JSONArray.parseArray(datas); //System.out.println(objects1); for (Object o : objects1) { JSONObject js1111 = JSONObject.parseObject(o.toString()); //System.out.println(js1111); String cells = js1111.get("cells").toString(); //System.out.println(cells); JSONArray objects2 = JSONArray.parseArray(cells); //System.out.println("objects2 : "+ objects2); for (Object o1 : objects2) { JSONObject js11111 = JSONObject.parseObject(o1.toString()); //System.out.println(js11111); String dataType = js11111.get("dataType").toString(); String value = js11111.get("value").toString(); Map<String, String> map = new HashMap<>(); map.put(dataType, value); list.add(map); //System.out.println("这他妈那个傻逼写的 dataType=" + dataType + " value="+ value); } } } System.out.println(list); } /** * 初始化list对象 */ public static Map<String, List<Student>> stduentInit() { Map<String, List<Student>> map = new HashMap<>(); List<Student> list = new ArrayList<>(); Student student = new Student(10, "马丹1", true); Student student1 = new Student(15, "马丹2", false); Student student2 = new Student(20, "马丹3", true); Student student3 = new Student(25, "马丹4", false); Student student4 = new Student(30, "马丹5", true); list.add(student); list.add(student1); list.add(student2); list.add(student3); list.add(student4); map.put("list", list); List<Student> list1 = new ArrayList<>(); Student stu = new Student(10, "马丹1", true); Student stu1 = new Student(15, "马丹2", false); Student stu2 = new Student(20, "马丹3", true); Student stu3 = new Student(25, "马丹4", false); Student stu4 = new Student(30, "马丹5", true); list1.add(stu); list1.add(stu1); list1.add(stu2); list1.add(stu3); list1.add(stu4); map.put("list1", list1); return map; } }
运行结果
key===>list1 list===>[Student(age=10, name=马丹1, sex=true), Student(age=15, name=马丹2, sex=false), Student(age=20, name=马丹3, sex=true), Student(age=25, name=马丹4, sex=false), Student(age=30, name=马丹5, sex=true)] list: 集合数组==》[马丹1, 马丹2, 马丹3, 马丹4, 马丹5] string: String数组==》马丹1,马丹2,马丹3,马丹4,马丹5 --------------------------------------1--------------------------------------------- collectle==>[Student(age=20, name=马丹3, sex=true), Student(age=25, name=马丹4, sex=false), Student(age=30, name=马丹5, sex=true)] ---------------------------------2-------------------------------------------------- strList: [马丹1, 马丹2, 马丹3, 马丹4, 马丹5] key===>list list===>[Student(age=10, name=马丹1, sex=true), Student(age=15, name=马丹2, sex=false), Student(age=20, name=马丹3, sex=true), Student(age=25, name=马丹4, sex=false), Student(age=30, name=马丹5, sex=true)] list: 集合数组==》[马丹1, 马丹2, 马丹3, 马丹4, 马丹5] string: String数组==》马丹1,马丹2,马丹3,马丹4,马丹5 --------------------------------------1--------------------------------------------- collectle==>[Student(age=20, name=马丹3, sex=true), Student(age=25, name=马丹4, sex=false), Student(age=30, name=马丹5, sex=true)] ---------------------------------2-------------------------------------------------- strList: [马丹1, 马丹2, 马丹3, 马丹4, 马丹5] [{2=闫文超}, {1=10.0}, {2=老王}, {1=20.0}]
文件内容