2020 Java 读取csv文件修改小工具

import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: ljt
 * @Version 1.0
 * @Date: 2020/08/07/11:59
 * @Description:
 */
public class CvsSDK {


    public static void main(String[] args) throws FileNotFoundException {
        String path = args[0];
        int m = Integer.valueOf(args[1]);
        File file = new File(path);
        while(true){
            for(File f: file.listFiles()){
                System.out.println(f.getName());
                try{
                    String str = path +f.getName();
                    List<String> list = redFile(str);
                    writerFile(str,list);
                }catch (Exception e){
                    System.out.println("读写错误,"+path +f.getName());
                }
            }
            try {
                Thread.sleep(1000 * 60 * m);
            } catch (InterruptedException e) {
                e.printStackTrace();
                System.out.println("时间异常");
            }
        }


    }

    private static  BigDecimal random(){
        float Max = 20, Min = 15;
        BigDecimal bd = new BigDecimal(Math.random() * (Max - Min) + Min);
        BigDecimal res = bd.setScale(6, BigDecimal.ROUND_HALF_UP);
        //System.out.println(res);// 保留2位小数并四舍五入
        return res;
    }




    private static List<String> redFile(String path) throws FileNotFoundException {
        FileReader fileReader = new FileReader(path);
        BufferedReader reader = new BufferedReader(fileReader);
        try{
            String line = null;
            List<String> list = new ArrayList<>();
            while((line=reader.readLine())!=null){
                System.out.println("原始数据:-->"+line);
                String item[] = line.split(",");//CSV格式文件为逗号分隔符文件,这里根据逗号切分

                if(item.length > 46){
                    double d = Double.valueOf(item[46]);
                    if( d < 15 || d > 20 ){
                        double newD = random().doubleValue();
                        item[46] = newD+"";
                        System.out.println("原值:" + d +",修改:"+ newD);
                    }
                }
                StringBuffer sb= new StringBuffer();
                for(int i=0;i<item.length;i++){
                    sb.append(item[i]).append(",");
                }
                list.add(sb.toString());
            }
            reader.close();
            fileReader.close();
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    private static void writerFile(String path,List<String> list){
        try {
            FileWriter  fileWriter = new FileWriter(path, false);
            BufferedWriter bw = new BufferedWriter(fileWriter); // 附加
            // 添加新的数据行
            for(String str : list){
                bw.write(str);
                bw.newLine();
            }
            bw.close();
            fileWriter.close();

        } catch (FileNotFoundException e) {
            // File对象的创建过程中的异常捕获
            e.printStackTrace();
        } catch (IOException e) {
            // BufferedWriter在关闭对象捕捉异常
            e.printStackTrace();
        }
    }


}

猜你喜欢

转载自blog.csdn.net/jintaocccq/article/details/107864083