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<String> head, List<List<String>> 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<String> 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<String> 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<String> getContent(String path){
    List<String> 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<Map<String,Object>> getContentJson(String path){
    List<Map<String,Object>> 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<String,Object> everyLine = new HashMap<>();
                String[] lines = line.split(",");
                for(int i=0;i<lines.length;i++){
                    if(i==1){
                        everyLine.put("t",lines[i]);
                    }
                    if(i==2){
                        everyLine.put("o",lines[i]);
                    }
                    if(i==3){
                        everyLine.put("a",lines[i]);
                    }
                    //String item = lines[i];
                }
                //System.out.println(everyLine);
                allString.add(everyLine);
            }

            c++;
        }
        System.out.println("csv表格中所有行数:"+allString.size());
    } catch (IOException e)
    {
        e.printStackTrace();
    }
    return allString;
}

/**
 * 压装
 * @param path
 * @return
 */
public static List<Map<String,Object>> getContentJson2(String path){
    List<Map<String,Object>> 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<String,Object> everyLine = new HashMap<>();
                String[] lines = line.split(",");
                for(int i=0;i<lines.length;i++){
                    if(i==1){
                        everyLine.put("t",lines[i]);
                    }
                    if(i==2){
                        everyLine.put("d",lines[i]);
                    }
                    if(i==3){
                        everyLine.put("p",lines[i]);
                    }
                    //String item = lines[i];
                }
                //System.out.println(everyLine);
                allString.add(everyLine);
            }

            c++;
        }
        System.out.println("csv表格中所有行数:"+allString.size());
    } catch (IOException e)
    {
        e.printStackTrace();
    }
    return allString;
}

public static void testData(){
    String catalogPath ="D:\\work\\data\\bfcec\\list.xlsx";

}

public static void main(String[] args) {
    String path = "D:\\work\\rootcloud\\tmp\\1.csv";
    List<String> 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<allString.size();i++){
        String contents = allString.get(i);
        String[] content = contents.split(",");
        Map<String, String> tags = new HashMap<>();
        Map<String, Object> fields = new HashMap<>();
        for(int j=0;j<head.length;j++){
            if(head[j].equals("index")){
                tags.put(head[j], content[j]);
            }else{
                Object v = content[j];
                fields.put(head[j], v);
            }
            System.out.println(head[j]+"="+content[j]);
        }
        System.out.println("--tags="+ JSON.toJSONString(tags)+"  fields="+JSON.toJSONString(fields));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
        Long time = null;
        try{
            String strT = fields.get("time").toString();
            Date date = sdf.parse(strT);
            Calendar calendar = Calendar.getInstance();

            calendar.setTime(date);
            Timestamp ts = Timestamp.valueOf(fields.get("time").toString());
            time = ts.getTime();
            String lastTime = strT.substring(strT.lastIndexOf(".")+1);
            String strTime = date.getTime()/1000+""+lastTime;
            time = Long.parseLong(strTime);
            System.out.println(" ----time="+fields.get("time").toString());
            System.out.println(" ----strTime="+strTime);
            System.out.println(" ----time="+ts.getTime());
        }catch (Exception e){
            e.printStackTrace();
        }
        influxDBConnection.insert("bfcec",tags,fields,time,TimeUnit.NANOSECONDS);
    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值