博客
关于我
如何在Java中生成大量随机CSV文件
阅读量:732 次
发布时间:2019-03-22

本文共 2853 字,大约阅读时间需要 9 分钟。

The CSV format remains one of the simplest and most widely used methods for data exchange. Its popularity has led to a constant need for developers to generate large volumes of CSV files for testing purposes. My latest open source project aims to address this need.

This project, , is a Java library designed to help developers generate randomized data files. Currently, it supports CSV and Fixed Width formats, with plans to expand to JSON formats in the future.

This guide will walk you through generating a simple CSV file using the library and the Faker library for random data generation.

Maven Integration

For Maven projects, you can add the required dependencies to your project's pom.xml file by including the following XML snippet:

au.com.anthonybruno        SdGen        0.3.0                com.github.javafaker        javafaker        0.14

Instructions

First, obtain an instance of the Faker class with the following code:

Faker faker = Faker.instance();

Next, use the Faker instance to generate values like URLs or names using methods such as faker.internet().url() or faker.space().planet().

To create the structure for your CSV file, use the SDGen builder. Start with:

Gen.start()

Then, add fields using the addField method. This method takes two parameters: the field name (used to identify the column in the generated file) and a generator (a simple interface with a single method to generate random values). Here's how to add "First Name" and "Last Name" fields:

Gen.start()    .addField("First Name", () -> faker.name().firstName())    .addField("Last Name", () -> faker.name().lastName())

For the "Age" field, you can use SDGen's built-in generator with specified min and max values:

Gen.start()    .addField("First Name", () -> faker.name().firstName())    .addField("Last Name", () -> faker.name().lastName())    .addField("Age", new IntGenerator(18, 80))

Next, specify the number of rows to generate and select the output format. For CSV files, you can use:

Gen.start()    .addField("First Name", () -> faker.name().firstName())    .addField("Last Name", () -> faker.name().lastName())    .addField("Age", new IntGenerator(18, 80))    .generate(1000)    .asCsv()

Finally, specify how to output the data. Use the toFile method to save the generated data to a file:

Gen.start()    .addField("First Name", () -> faker.name().firstName())    .addField("Last Name", () -> faker.name().lastName())    .addField("Age", new IntGenerator(18, 80))    .generate(1000)    .asCsv()    .toFile("people.csv");

That's it! Running the code will generate a CSV file in your project's working directory. Here's an example of the generated data:

First Name,Last Name,Age  
Corrine,Berge,78
Gerald,Carter,63
Enid,Padberg,66
Eleanora,Murray,79
Coy,Okuneva,76
Jovan,Reynolds,77
Lane,Haag,48

For more details about SDGen, visit its official website.

转载地址:http://qmwwk.baihongyu.com/

你可能感兴趣的文章
mt_rand
查看>>
mysql -存储过程
查看>>
mysql /*! 50100 ... */ 条件编译
查看>>
mudbox卸载/完美解决安装失败/如何彻底卸载清除干净mudbox各种残留注册表和文件的方法...
查看>>
mysql 1264_关于mysql 出现 1264 Out of range value for column 错误的解决办法
查看>>
mysql 1593_Linux高可用(HA)之MySQL主从复制中出现1593错误码的低级错误
查看>>
mysql 5.6 修改端口_mysql5.6.24怎么修改端口号
查看>>
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>