代码编织梦想

场景说明

场景说明:

基于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>

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_17040587/article/details/129667218

Hutool工具类-爱代码爱编程

最新依赖 <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</a

Hutool Java常用工具类汇总-爱代码爱编程

简介 Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。 Hutool中的工具方法来自于每个用户的精雕细琢,它涵盖了Java开发底层代码中的方方面面,它既是大型项目开发中解决小问题的利器,也是小型项目中的效率担当; Hutool是项

Hutool是一个小而全的Java工具类库-爱代码爱编程

1、Hutool简介 Hutool 是一个小而全的 Java 工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使 Java 拥有函数式语言般的优雅,让 Java 语言也可以“甜甜的”。 Hutool 中的工具方法来自每个用户的精雕细琢,它涵盖了 Java 开发底层代码中的方方面面,它既是大型项目开发中解决小问题的利器,也是小型项目中

Java开发人员必知的常用类库,这些你都知道吗?-爱代码爱编程

作为一名程序员,我们要避免重复发明轮子,尽可能使用一些成熟、优秀、稳定的的第三方库,站在巨人的肩膀上搭建可靠、稳定的系统。本篇我整理了Java开发人员经常会使用到的第三方类库,可能不是很全面,还在持续收集整理中,朋友们可以关注我的GitHub上的持续更新,GitHub搜wind7rui/Javalib,或者点击链接 https://github.com/w

文件上传与下载(本地和远程)-爱代码爱编程

Controllerpackage com.ydtech.modules.admin.controller; import com.ydtech.core.page.HttpResult; import com.ydtech.modules.esm.service.UpdownLoadService; import io.swagger.annotati

Java开发人员必知的常用类库,你合格了吗?-爱代码爱编程

作为一名程序员,我们要避免重复发明轮子,尽可能使用一些成熟、优秀、稳定的的第三方库,站在巨人的肩膀上搭建可靠、稳定的系统。本篇我整理了Java开发人员经常会使用到的第三方类库,可能不是很全面,还在持续收集整理中,朋友们可以关注我的GitHub上的持续更新,GitHub搜wind7rui/Javalib,或者点击链接 https://gith

常用工具类hutool的学习使用_我是一个小仓鼠01的博客-爱代码爱编程

简介 中文官网:https://plus.hutool.cn/docs/ Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语

java-poi && easyexcel_@autowire的博客-爱代码爱编程

https://easyexcel.opensource.alibaba.com/docs/current/quickstart/write#web%E4%B8%AD%E7%9A%84%E5%86%99 Apache PO

springboot从sftp读取csv文件_kevin-anycode的博客-爱代码爱编程

需求描述:从SFTP读取csv文件写入本地数据库中 1. SFTP服务器配置 application.yml sftp: host: 127.0.0.1 passWord: 123456 port: 22

如何优雅的用poi导入excel文件-爱代码爱编程

在企业级项目开发中,要经常涉及excel文件和程序之间导入导出的业务要求,那么今天来讲一讲excel文件导入的实现。java实现对excel的操作有很多种方式,例如EasyExcel等,今天我们使用的是POI技术实现excel文件的导入。 POI技术简介 1.POI概念 Apache POI 是用Java编写的免费开源的跨平台的Java AP

jvm监控搭建-爱代码爱编程

文章目录 JVM监控搭建整体架构JolokiaTelegrafInfluxdbGrafana JVM监控搭建 整体架构 JVM 的各种内存信息,会通过 JMX 接口进行暴露。 Jolokia

简单明了实现java地图小程序项目_java地图开发-爱代码爱编程

简单明了实现Java地图小程序项目 ✨博主介绍前言地图概述地图技术地图应用场景网约车服务智能穿戴智能物流智能景区车联网 国内常见地图地图API与搜索JS API GL(演示百度地图)创建浏览器

报表技术2(百万数据导入导出,poi操作word)_poi模板导出word-爱代码爱编程

POI模板导出,操作word 导出用户详情数据(图片,公式处理)使用模板导出用户详细信息使用模板引擎1.编写模板引擎2.使用模板引擎 百万数据导出代码实现: 百万数据导入步骤分析:1.自定义

java中csv文件读写分析_java csv-爱代码爱编程

文章目录 一、txt、csv、tsv文件二、csv文件规范三、csv使用场景四、Java中的csv类库1. javacsv2. opencsv写入器读取器解析器注解映射策略MappingStrategy接口Ma