采用JavaCSV将数据转CSV文件

2019-04-23  本文已影响0人  JiangCheng97

采用JavaCSV将数据转CSV文件

Pom.xml引入依赖

<dependency>
    <groupId>net.sourceforge.javacsv</groupId>
    <artifactId>javacsv</artifactId>
    <version>2.0</version>
</dependency>

源码

package com.weather.demo.uitls;


import com.csvreader.CsvWriter;
import com.weather.demo.model.Weather;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class CSVUtils {

    /**
     * 写CSV文件
     * @param fileName  文件名
     * @param weather   天气
     * @param title     列头
     */
    public void writeCSV(String fileName, Weather weather,String[] title){

        Boolean flag  = false;
        //获取文件夹名
        String dir = fileName.substring(0, 11);

        //判断文件夹是否存在
        File dirFile = new File(dir);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }

        File csvFile = new File(fileName);

        //判断文件是否存在
        if (!csvFile.exists()) {
            flag = true;
            try {
                csvFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        FileWriter fileWriter = null;
        BufferedWriter bufferedWriter = null;
        CsvWriter csvWriter = null;

        try{
            fileWriter = new FileWriter(csvFile,true);
            bufferedWriter = new BufferedWriter(fileWriter);
            //创建CSV写对象
            csvWriter = new CsvWriter(bufferedWriter,';');

            //如果文件是第一次创建 写入列头
            if(flag==true){
                csvWriter.writeRecord(title);
            }

            List<String> weatherDataRecord = new ArrayList<>();
            weatherDataRecord.add(weather.getWeather());
            weatherDataRecord.add(weather.getTemp());
            weatherDataRecord.add(weather.getWater());
            weatherDataRecord.add(weather.getWind());
            weatherDataRecord.add(sdf.format(weather.getForcastDate()));

            //设置整点时间
            calendar.setTime(weather.getForcastDate());
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            Date time = calendar.getTime();
            weatherDataRecord.add(sdf.format(time));

            //将List转String数组 写入文件中
            csvWriter.writeRecord(weatherDataRecord.toArray(new String[weatherDataRecord.size()]));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try{
                //关闭写
                bufferedWriter.close();
                fileWriter.close();
                csvWriter.close();
            } catch (IOException e){
                e.printStackTrace();
            }
        }

    }
}

上一篇下一篇

猜你喜欢

热点阅读