java批处理csv文件简单demo

import java.io.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class ProcessCSV {
    public static void main(String[] args) {
        File inputFile = new File("/Users/sunxianyan/Desktop/input.csv");
        File outputFile = new File("/Users/sunxianyan/Desktop/output.csv");
        List<String> dataList = ProcessCSV.importCsv(inputFile);
        List<String> outputList=new ArrayList<String>();
        if(dataList!=null && !dataList.isEmpty()) {
            for (int i = 0; i < dataList.size(); i++) {
                String s = dataList.get(i);
                String[] as = s.split(",");
                String [] str = as[0].split(".com/");
                String targetUrl = "http://xxx.com/" + str[str.length-1];
                outputList.add(targetUrl+","+as[1]);
                boolean isSuccess=ProcessCSV.exportCsv(outputFile, outputList);
                System.out.println(isSuccess+":"+targetUrl+","+as[1]);
            }
        }
    }

    /**
     * 写入
     * @param file csv文件(路径+文件名),csv文件不存在会自动创建
     * @param dataList 数据
     * @return
     */
    public static boolean exportCsv(File file, List<String> dataList){
        boolean isSucess=false;

        FileOutputStream out=null;
        OutputStreamWriter osw=null;
        BufferedWriter bw=null;
        try {
            out = new FileOutputStream(file);
            osw = new OutputStreamWriter(out);
            bw =new BufferedWriter(osw);
            if(dataList!=null && !dataList.isEmpty()){
                for(String data : dataList){
                    bw.append(data).append("\r");
                }
            }
            isSucess=true;
        } catch (Exception e) {
            isSucess=false;
        }finally{
            if(bw!=null){
                try {
                    bw.close();
                    bw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(osw!=null){
                try {
                    osw.close();
                    osw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out!=null){
                try {
                    out.close();
                    out=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return isSucess;
    }

    /**
     * 读取
     * @param file csv文件(路径+文件)
     * @return
     */
    public static List<String> importCsv(File file){
        List<String> dataList=new ArrayList<String>();

        BufferedReader br=null;
        try {
            br = new BufferedReader(new FileReader(file));
            String line = "";
            while ((line = br.readLine()) != null) {
                dataList.add(line);
            }
        }catch (Exception e) {
        }finally{
            if(br!=null){
                try {
                    br.close();
                    br=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return dataList;
    }
}

 

posted @ 2019-07-09 10:24  yangyuxiaozi  阅读(282)  评论(0编辑  收藏  举报