package main.java.company.utils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
* @program: testDevelop
* @ClassName: CSVUtils2
* @version: 1.0
* @description:
* @author: zhaonian
* @create: 2021-01-29 20:33
public class CSVUtils2 {
* CSV文件列分隔符
private static final String CSV_COLUMN_SEPARATOR = ",";
* CSV文件列分隔符
private static final String CSV_RN = "\r\n";
public static void main(String[] args) {
exportCsv();
private static List<List<Object>> getNovel() {
List<List<Object>> dataList = new ArrayList<List<Object>>();
List<Object> rowList = null;
for (int i = 0; i < 165; i++) {
rowList = new ArrayList<Object>();
Object[] row = new Object[4];
row[0] = i;
row[1] = "风云第一刀" + i + System.lineSeparator() + "风云第一刀999";
row[2] = "古龙" + i + "";
row[3] = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
for (int j = 0; j < row.length; j++) {
rowList.add(row[j]);
dataList.add(rowList);
return dataList;
public static void exportCsv() {
long startTime = System.currentTimeMillis();
Object[] head = {"序号", "小说名称", "作者", "出版日期"};
List<Object> headList = Arrays.asList(head);
List<List<Object>> dataList = getNovel();
String downloadFilePath = "D://";
String fileName = "download";
File csvFile = CSVUtils2.createCSVFile(headList, dataList, downloadFilePath, fileName);
String fileName2 = csvFile.getName();
System.out.println(fileName2);
long endTime = System.currentTimeMillis();
System.out.println("整个CSV导出" + (endTime - startTime)+"毫秒");
* CSV文件生成方法
* @param head
* @param dataList
* @param outPutPath
* @param filename
* @return
public static File createCSVFile(List<Object> head, List<List<Object>> dataList, String outPutPath, String filename) {
File csvFile = null;
BufferedWriter csvWtriter = null;
try {
csvFile = new File(outPutPath + File.separator + filename + ".csv");
File parent = csvFile.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
csvFile.createNewFile();
csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "GB2312"), 1024);
writeRow(head, csvWtriter);
for (List<Object> row : dataList) {
writeRow(row, csvWtriter);
csvWtriter.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvWtriter.close();
} catch (IOException e) {
e.printStackTrace();
return csvFile;
* 写一行数据方法
* @param row
* @param csvWriter
* @throws IOException
private static void writeRow(List<Object> row, BufferedWriter csvWriter) throws IOException {
for (Object data : row) {
StringBuffer buf = new StringBuffer();
String rowStr = buf.append("\"").append(data).append("\t\",").toString();
csvWriter.write(rowStr);
csvWriter.newLine();
package company.utils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.druid.util.StringUtils;
import javax.servlet.http.HttpServletResponse;
* 文件操作
public class CSVUtils {
* 功能说明:获取UTF-8编码文本文件开头的BOM签名。
* BOM(Byte Order Mark),是UTF编码方案里用于标识编码的标准标记。例:接收者收到以EF BB BF开头的字节流,就知道是UTF-8编码。
* @return UTF-8编码文本文件开头的BOM签名
public static String getBOM() {
byte b[] = {(byte)0xEF, (byte)0xBB, (byte)0xBF};
return new String(b);
* 生成CVS文件
* @param exportData
* 源数据List
* @param map
* csv文件的列表头map
* @param outPutPath
* 文件路径
* @param fileName
* 文件名称
* @return
@SuppressWarnings("rawtypes")
public static File createCSVFile(List exportData, LinkedHashMap map, String outPutPath,
String fileName) {
File csvFile = null;
BufferedWriter csvFileOutputStream = null;
try {
File file = new File(outPutPath);
if (!file.exists()) {
file.mkdirs();
csvFile =new File(outPutPath+fileName+".csv");
file.createNewFile();
csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
csvFile), "UTF-8"), 1024);
csvFileOutputStream.write(getBOM());
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
csvFileOutputStream.write((String) propertyEntry.getValue() != null ? (String) propertyEntry.getValue() : "" );
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
csvFileOutputStream.newLine();
for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
Object row = (Object) iterator.next();
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator
.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
.next();
String str=row!=null?((String)((Map)row).get( propertyEntry.getKey())):"";
if(StringUtils.isEmpty(str)){
str="";
}else{
str=str.replaceAll("\"","\"\"");
if(str.indexOf(",")>=0){
str="\""+str+"\"";
csvFileOutputStream.write(str);
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
if (iterator.hasNext()) {
csvFileOutputStream.newLine();
csvFileOutputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvFileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
return csvFile;
* 生成并下载csv文件
* @param response
* @param exportData
* @param map
* @param outPutPath
* @param fileName
* @throws IOException
@SuppressWarnings("rawtypes")
public static void exportDataFile(HttpServletResponse response, List exportData, LinkedHashMap map, String outPutPath, String fileName) throws IOException{
File csvFile = null;
BufferedWriter csvFileOutputStream = null;
try {
File file = new File(outPutPath);
if (!file.exists()) {
file.mkdirs();
csvFile =new File(outPutPath+fileName+".csv");
if(csvFile.exists()){
csvFile.delete();
csvFile.createNewFile();
csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "UTF-8"), 1024);
csvFileOutputStream.write(getBOM());
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
csvFileOutputStream.write((String) propertyEntry.getValue() != null ? (String) propertyEntry.getValue() : "" );
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
csvFileOutputStream.newLine();
for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
Object row = (Object) iterator.next();
for (Iterator propertyIterator = map.entrySet().iterator(); propertyIterator
.hasNext();) {
java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator
.next();
String str=row!=null?((String)((Map)row).get( propertyEntry.getKey())):"";
if(StringUtils.isEmpty(str)){
str="";
}else{
str=str.replaceAll("\"","\"\"");
if(str.indexOf(",")>=0){
str="\""+str+"\"";
csvFileOutputStream.write(str);
if (propertyIterator.hasNext()) {
csvFileOutputStream.write(",");
if (iterator.hasNext()) {
csvFileOutputStream.newLine();
csvFileOutputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
csvFileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
InputStream in = null;
try {
in = new FileInputStream(outPutPath+fileName+".csv");
int len = 0;
byte[] buffer = new byte[1024];
OutputStream out = response.getOutputStream();
response.reset();
response.setContentType("application/csv;charset=UTF-8");
response.setHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(fileName+".csv", "UTF-8"));
response.setCharacterEncoding("UTF-8");
while ((len = in.read(buffer)) > 0) {
out.write(new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF });
out.write(buffer, 0, len);
out.close();
} catch (FileNotFoundException e) {
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
throw new RuntimeException(e);
* 删除该目录filePath下的所有文件
* @param filePath
* 文件目录路径
public static void deleteFiles(String filePath) {
File file = new File(filePath);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
files[i].delete();
* 删除单个文件
* @param filePath
* 文件目录路径
* @param fileName
* 文件名称
public static void deleteFile(String filePath, String fileName) {
File file = new File(filePath);
if (file.exists()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()) {
if (files[i].getName().equals(fileName)) {
files[i].delete();
return;
* 测试数据
* @param args
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) {
List exportData = new ArrayList<Map>();
Map row1 = new LinkedHashMap<String, String>();
row1.put("1", "1,2,3,4"+System.lineSeparator()+"6,7,8");
row1.put("2", "12");
row1.put("3", "13");
row1.put("4", "14");
exportData.add(row1);
row1 = new LinkedHashMap<String, String>();
row1.put("1", "21");
row1.put("2", "22");
row1.put("3", "23");
row1.put("4", "24");
exportData.add(row1);
LinkedHashMap map = new LinkedHashMap();
map.put("1", "第一列名称");
map.put("2", "第二列名称");
map.put("3", "第三列名称");
map.put("4", "第四列名称");
String path = "D:/";
String fileName = "文件导出";
File file = CSVUtils.createCSVFile(exportData, map, path, fileName);
String fileName2 = file.getName();
System.out.println("文件名称:" + fileName2);
文章目录csv文件操作工具类csv文件操作工具类package company.utils;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStr
1、面数据的处理在arcgis中,将wgs84坐标系下的面,生成所有的折点所有的折点添加x和y字段,并且计算各点的坐标最终导出为csv文件,格式如下所示2、java读取面文件先定义文件路径public static String datadir = "Y:--数据处理-2出租车数据处理01数据";
public static String areafile = datadir + "00地理数据0...
开发的时候需要导出一批数据,为了方便使用的CSV格式。当时就只是简单的用逗号分隔,但是因为部分字段含有换行符、引号、逗号,导致用Excel打开的时候部分数据错位了,于是又将那几个字段处理了一下。
CSV文件本质是一种用逗号和(回车)换行符分割的文本文件,是可以直接中Excel打开的。
处理方式就是在这个字段前后添加双引号,并且将字段中原有的双引号替换为两个双引号。
* @author pzzhao
* @version 创建时间:2022-5-8 14:46
public class
1. CSV的换行符号要使用CRLF既" 回车符+换行符"的形式.
2. 文字可以使用双引号围起来, 逗号可以围在双引号里面
3. 每个单引号要换成""(两个单引号)且字段要用一对单引号围住
简单的说,就是csv内容...
本文讲述如何用java来写csv文件。 CSV的意思是逗号分隔符(Comma-Separated-Values),是不同系统之间传输数据的一种常见方式。
要想写csv文件需要用到java.io包。本文将讲述如何处理特殊字符。我们的目标是写出Microsoft Excel和google sheets可以读取的csv文件。
给出java例子后,我们还将给出一些好用的第三方库。
public class ExportTxt {
public static void main(String[] args) {
String content = "Hello, world!\r\nThis is a new line.";
String filePath = "C:\\example.txt";
try {
File file = new File(filePath);
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("File saved successfully.");
} catch (IOException e) {
e.printStackTrace();
在上面的代码中,我们首先定义了要写入文件的内容和文件路径。然后,我们创建一个FileWriter和BufferedWriter对象来写入文件,并使用"\r\n"来实现换行。最后,我们关闭BufferedWriter并打印出保存文件成功的消息。
Disconnected from the target VM, address: ‘127.0.0.1:xxxxx’, transport: ‘socket’
qaz139070:
springboot-综合案例
猪小屁嘟嘟嘟:
Spring Boot整合swagger-bootstrap-ui
拉面牛奶:
idea快速复制一个项目并改变端口号
拉面牛奶: