基于hutool生成csv文件

场景说明

场景说明:

基于hutool 生成csv文件 随机文件名【getRandomFileName()】
指定路径【path】下生成csv 这里引用了一个循环模拟数据接收 这个数据来源可以是rabbitMQ、redis等 一次性获取多条数据推送
每条数据生成一行csv 并且csv有自己的表头
当读取csv的行量【readSize()】超过最大值【csvMaxSize】时 重新生成一个带表头的文件createCsv();
如果行量没超过最大值时 继续追加数据 由于hutool没有提供直接的接口方法做追加数据:这里采纳了hutool读取csv数据【readData()】 合并新数据模式写入文件
循环推送 做了34次推送 每次推两条
由于推送每次是多条 判断当前如果小于最大值【csvMaxSize】时 就会当前量【readSize()】+ 每次推送量 会使文件实际行量大于等于最大值【csvMaxSize】 下一次推送时才能发现行量超过最大值【csvMaxSize】 出发生成新文件

测试方法

测试方法:

搞一个空maven项目
放上我的pom 直接放 不用的也可以自己删吧删吧
把java代码建一个TestController 为了测试方便 一大坨都给你放这里头了
运行
访问 http://127.0.0.1:8080/swagger-ui.html

代码


具体代码如下:

代码:

package com.dragon.controller;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.csv.*;


import cn.hutool.core.util.CharsetUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;


@Api(value = "测试")
@RestController
@RequestMapping("/test")
public class TestController {

    private String csvName;

    private String path= "D:\\cscFile\\";

    private int csvMaxSize=20;


    @Data
    @Setter
    @Getter
    public class User {

        private String id;
        private String name;
        private String nickname;
        private String sex;
        private String age;

    }


    /**
     * 获取并组装导出数据
     */
    @ApiOperation(value = "测试连通性", httpMethod = "GET")
    @ResponseBody
    @RequestMapping(value = "/p", method = RequestMethod.GET)
    public void getUserList() {

        //创建文件件 放置表头
        createCsv();

        //模拟每次接受消息
        for(int i=1;i<35;i++){

            //模拟数据环节
            List<User> userList=new ArrayList<>();
            User tempA  = new User();
            //模拟每次推两条
            tempA.setId("AID-A:"+Integer.toString(i));
            tempA.setSex("A男");
            tempA.setName("Aname:wanger");
            tempA.setNickname("Anickname:Jay");
            tempA.setAge("A30");
            userList.add(tempA);
            User tempB  = new User();
            tempB.setId("BID-B:"+Integer.toString(i));
            tempB.setSex("B男");
            tempB.setName("Bname:wanger");
            tempB.setNickname("Bnickname:Jay");
            tempB.setAge("B30");
            userList.add(tempB);

            //检查csv行数 超过规定重建文件
            List result= new ArrayList<String[]>();
            if(readSize(path+csvName) >csvMaxSize){
                createCsv();
            }
            //先读取文件内容 列宽
            List<CsvRow> fileCsvRows = new ArrayList<>();
            fileCsvRows = readData(path+csvName);
            if(fileCsvRows.size()>0){
                for(int lineKey=0;lineKey<fileCsvRows.size();lineKey++){
                    result.add(fileCsvRows.get(lineKey));
                }

            }
            //保存开始------------------------

            for(User user:userList){
                //遍历用户列表的数据 列数
                Field[] declaredFields =new Field[4];
                try {
                    //将对象要显示的字段插入对应标题位置
                    declaredFields[0]=user.getClass().getDeclaredField("name");
                    declaredFields[1]=user.getClass().getDeclaredField("nickname");
                    declaredFields[2]=user.getClass().getDeclaredField("sex");
                    declaredFields[3]=user.getClass().getDeclaredField("id");

                    //获取UserList对象的数据作为listValue
                    String[] listValue = new String[declaredFields.length];
                    for(int x=0;x<listValue.length;x++){
                        declaredFields[x].setAccessible(true);  //设置为允许操作
                        //将属性值添加到listValue对应的索引位置下
                        listValue[x] = String.valueOf(declaredFields[x].get(user)) ;
                        //将listValue添加到result中
                    }
                    result.add(listValue);

                } catch (Exception  e) {
                    e.printStackTrace();
                }
            }
            //将组装好的结果集传入导出csv格式的工具类
            this.createCsvFile(result,csvName);

            //保存结束------------------------
        }



    }

    public void createCsv(){
        csvName=getRandomFileName("dzbs-")+".csv";
        //自定义标题别名,作为第一行listName
        String[] listName = new String[4];
        listName[0]="用户名";
        listName[1]="用户昵称";
        listName[2]="性别";
        listName[3]="id";
        //获取String[]类型的数据至result中
        List newResult= new ArrayList<String[]>();
        //将listName添加到result中
        newResult.add(listName);
        this.createCsvFile(newResult,csvName);
    }

    /**
     * 导出csv格式工具类
     * 创建文件并放置数据  这里放置了表头一行数据
     * @param result 导出数据
     * @param fileName 文件名
     */
    public void createCsvFile(List result,String fileName){
        try {
            File csvFile = new File(fileName);  //构造文件
            //导入HuTool中CSV工具包的CsvWriter类
            //设置导出字符类型, CHARSET_UTF_8
//            CsvWriter writer = CsvUtil.getWriter(csvFile, CharsetUtil.CHARSET_UTF_8);
            CsvWriter writer = CsvUtil.getWriter(csvFile, CharsetUtil.CHARSET_GBK);
            writer.write(result);  //通过CsvWriter中的write方法写入数据
            writer.close();   //关闭CsvWriter
            //保存文件
            FileInputStream fileInputStream=new FileInputStream(csvFile);
            this.saveFile(fileInputStream,csvFile.getName());
        }
        catch (Exception e){
            System.out.println(e);
        }
    }

    /**
     * 文件保存 覆盖保存
     * @param inputStream
     * @param fileName
     */
    private void saveFile(InputStream inputStream, String fileName) {
        OutputStream os = null;
        try {
            //保存文件路径
            // 1K的数据缓冲 1024000 1M
            byte[] bs = new byte[1024000];
            // 读取到的数据长度
            int len;
            // 输出的文件流保存到本地文件
            File tempFile = new File(path);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }
            os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
            // 开始读取
            while ((len = inputStream.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 完毕,关闭所有链接
            try {
                os.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    /* *
     * 检查文件是否存在
     * 读取数据封装到实体 读取行数
     */
    private int readSize(String fileName) {
        CsvReader reader = CsvUtil.getReader();
        //从文件中读取CSV数据
        int size = 0;
        try {
            CsvData data = reader.read(FileUtil.file(fileName), CharsetUtil.CHARSET_GBK);
            size = data.getRowCount();
        }catch (Exception e){
            System.out.println("尚未生成:"+fileName+ " --即将创建 ");
        }
        return size;

    }

    /* *
     * 读取数据封装到实体
     */
    private  List<CsvRow> readData(String fileName) {
        CsvReader reader = CsvUtil.getReader();
        List<CsvRow> scvData = new ArrayList<>();

        try {
            CsvData data = reader.read(FileUtil.file(fileName), CharsetUtil.CHARSET_GBK);
            scvData = data.getRows();

        }catch (Exception e){
//            System.out.println("尚未生成:"+fileName+ " --即将创建 ");
        }

        return scvData;
    }



    public static String getRandomFileName(String pre) {
        SimpleDateFormat simpleDateFormat;

        simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

        Date date = new Date();

        String str = simpleDateFormat.format(date);

        Random random = new Random();

        int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数

        return  pre+ str + rannum;// 当前时间

    }


}


依赖


maven依赖:

<?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.5.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.dragon</groupId>
    <artifactId>springboot_rabbitmq_consumer</artifactId>
    <version>v1.3</version>
    <packaging>jar</packaging>
    <name>springboot_rabbitmq_consumer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-api</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j-to-slf4j</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.16.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-api</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.16.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-to-slf4j</artifactId>
            <version>2.16.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>log4j-api</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>mybatis-plus-core</artifactId>
                    <groupId>com.baomidou</groupId>
                </exclusion>
            </exclusions>
        </dependency>


        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.6.5</version>
        </dependency>



        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-core</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.2.3</version>
            <classifier>jdk15</classifier>
            <exclusions>
                <exclusion>
                    <artifactId>commons-lang</artifactId>
                    <groupId>commons-lang</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>commons-logging</artifactId>
                    <groupId>commons-logging</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.78</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>lib</directory>
                <targetPath>BOOT-INF/lib/</targetPath>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>

            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>

</project>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

溜达的大象

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值