实时读取文件数目会变化的csv文件

import com.opencsv.CSVReader;
import com.opencsv.exceptions.CsvValidationException;
import java.io.*;
import java.util.ArrayList;

public class csv {
    public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
        //指定文件夹
        String directoryPath = "D:\\Program Files (x86)\\idea\\flink-kafka\\src\\main\\stock";
        File directory = new File(directoryPath);
        ArrayList<Integer> row = new ArrayList<>();//记录csv文件读到了多少行
        int sum=1;
        while (true){
            int csv=0;//csv文件的个数也代表当前是第几个csv
            for (File file : directory.listFiles()) {
                if (file.getName().endsWith(".csv")){
                    csv++;
                    CSVReader reader = new CSVReader(new InputStreamReader(new FileInputStream(file), "GBK"));
                    String[] stock; // 存储每一行数据的数组
                    int now;//行数
                    int read=1;//跳过前面的行数
                    try {
                        now=row.get(csv-1)+1;//若数组里已经存了当前行数则替换
                    }
                    catch (IndexOutOfBoundsException e){
                        now=1;
                    }

                    try {
                        int r=0;
                        System.out.println("新增数据:");
                        while ((stock = reader.readNext()) != null) {
                            if (now==1){
                                now--;//第一行是标签
                                continue;
                            }
                            // 处理每一行数据
                            if (read<=now){
                                read++;
                            }
                            else {
                                r++;
                                String value = "";
                                for (int i=0;i< stock.length;i++) {
                                    if(i== stock.length-1)
                                        value = value + stock[i];
                                    else
                                        value = value + stock[i]+ ",";
                                }
                                System.out.println(value);
                            }
                        }
                        try{
                            row.set(csv-1,r+row.get(csv-1));
                        }
                        catch (IndexOutOfBoundsException e){
                            row.add(r);
                        }

                    }
                    catch (IOException e){
                        System.out.println("文件不存在或打开失败");
                    }
                    catch (CsvValidationException e){
                        System.out.println("行数与预期的列数不匹配或CSV文件格式不正确,无法解析或该行包含了不正确的数据");
                    }
                    System.out.println("第"+sum+"次"+"读取完毕"+file+'\n'+"当前"+row.get(csv-1)+'行');
                    System.out.println("------------------------------------------");
                }
            }
            try {
                // 暂停当前线程执行1秒钟
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (csv==0)
                System.out.println("第"+sum+"次读取"+"  没有数据");
            sum++;
        }
    }
}

结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值