csv文件生成

package com.rootcloud.bfcec.util;

import com.alibaba.fastjson.JSON;

import java.io.;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.
;
import java.util.concurrent.TimeUnit;

public class CsvUtil {

/**
 * CSV文件生成方法
 * @param head
 * @param dataList
 * @param outPutPath
 * @param filename
 * @return
 */
public static File createCSVFile(List head, List> 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();

        // GB2312使正确读取分隔符","
        csvWtriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
                csvFile), "GBK"), 1024);
        // 写入文件头部
        writeRow(head, csvWtriter);

        // 写入文件内容
        for (List row : dataList) {
            writeRow(row, csvWtriter);
        }
        csvWtriter.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            csvWtriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return csvFile;
}

private static void writeRow(List row, BufferedWriter csvWriter) throws IOException {
    // 写入文件头部
    for (Object data : row) {
        StringBuffer sb = new StringBuffer();
        String rowStr = sb.append("\"").append(data).append("\",").toString();
        csvWriter.write(rowStr);
    }
    csvWriter.newLine();
}




public static List getContent(String path){
    List allString = new ArrayList<>();
    File csv = new File(path);  // CSV文件路径
    BufferedReader br = null;
    try
    {
        br = new BufferedReader(new FileReader(csv));
    } catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    String line = "";
    String everyLine = "";
    try {

        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();
    }
    return allString;
}

public static List> getContentJson(String path){
    List> allString = new ArrayList<>();
    File csv = new File(path);  // CSV文件路径
    BufferedReader br = null;
    try
    {
        br = new BufferedReader(new FileReader(csv));
    } catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    String line = "";

    try {
        int c = 0;
        while ((line = br.readLine()) != null)  //读取到的内容给line变量
        {
            if(c!=0){
                Map everyLine = new HashMap<>();
                String[] lines = line.split(",");
                for(int i=0;i> getContentJson2(String path){
    List> allString = new ArrayList<>();
    File csv = new File(path);  // CSV文件路径
    BufferedReader br = null;
    try
    {
        br = new BufferedReader(new FileReader(csv));
    } catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    String line = "";

    try {
        int c = 0;
        while ((line = br.readLine()) != null)  //读取到的内容给line变量
        {
            if(c!=0){
                Map everyLine = new HashMap<>();
                String[] lines = line.split(",");
                for(int i=0;i allString = CsvUtil.getContent(path);
    String heads = allString.get(0);
    String[] head = heads.split(",");
    InfluxDBConnection influxDBConnection = new InfluxDBConnection("admin", "admin", "http://127.0.0.1:8086", "mydb", "autogen");
    for(int i=1;i tags = new HashMap<>();
        Map fields = new HashMap<>();
        for(int j=0;j

}

你可能感兴趣的:(csv文件生成)