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++;
}
}
}
结果: