首页 > 解决方案 > 如何使用java仅基于第一列元素复制.CSV的一部分

问题描述

像这样复制部分(从日期到日期)我试图根据第一列(开始日期和时间)数据仅复制 .CSV 文件的一部分(2019-01-28 10:22:00 AM)但是用户必须这样说(2019/01/28 10:22:00)

这适用于 windows,java opencsv,这是我找到的,但不做我需要的 exaclty:

像这样:

int startLine = 从列 csv 中获取 value1 ;int endLine = 从列 csv 中获取 value2;

public static void showLines(String fileName, int startLine, int endLine) throws IOException  {
    String line = null;
    int currentLineNo = 1;
//  int startLine = 20056;//40930;
//  int currentLineNo = 0;
    File currentDirectory = new File(new File(".").getAbsolutePath());
    String fromPath = currentDirectory.getCanonicalPath() + "\\Target\\part.csv";
    PrintWriter pw = null;

        pw = new PrintWriter(new FileOutputStream(fromPath), true);
        //pw.close();
    BufferedReader in = null;
    try {
        in = new BufferedReader (new FileReader(fileName));

        //read to startLine
        while(currentLineNo<startLine) {
            if (in.readLine()==null) {
                // oops, early end of file
                throw new IOException("File too small");
            }
            currentLineNo++;
        }

        //read until endLine
        while(currentLineNo<=endLine) {
            line = in.readLine();
            if (line==null) {
                // here, we'll forgive a short file
                // note finally still cleans up
                return;
            }
            System.out.println(line);
            currentLineNo++;
            pw.println(line);
        }

    } catch (IOException ex) {


        System.out.println("Problem reading file.\n" + ex.getMessage());

    }finally {
        try { if (in!=null) in.close();
        pw.close();
        } catch(IOException ignore) {}
    }
}


public static void main(String[] args) throws FileNotFoundException {

    int startLine = 17 ;
    int endLine = 2222;
    File currentDirectory = new File(new File(".").getAbsolutePath());
    try {
        showLines(currentDirectory.getCanonicalPath() +  "\\Sources\\concat.csv", startLine, endLine);
    } catch (IOException e) {

        e.printStackTrace();
    }
//  pw.println();

}

标签: java

解决方案


 public static void main(String[] args) throws IOException {
            File currentDirectory = new File(new File(".").getAbsolutePath());
String targetPath = currentDirectory.getCanonicalPath() + "\\Target\\part.csv";
String inputFilePath = currentDirectory.getCanonicalPath() +  "\\Sources\\concat.csv";
            String line = null;
            ArrayList<String> lines = new ArrayList<String>();
            boolean add = false;
            String startLine = "2019/01/28 10:22:00";
            String endLine = "2019/04/06 10:30:00";
           try(PrintWriter pw = new PrintWriter(new FileOutputStream(targetPath), true)){
 try (BufferedReader input = new BufferedReader(new FileReader(inputFilePath))){
                    while((line = input.readLine()) != null) {
                        String date = line.split(",")[0];
                        if(date.contains(startLine)) {
                            add = true;
                        }else if(date.contains(endLine)) {
                            break;
                        }
                        if(add) {
                            lines.add(line);
                        }
                    }
                }
                for(String currentLine : lines) {
                    pw.append(currentLine + "\n");
                }
            }catch(FileNotFoundException e) {
                 e.printStackTrace();
             }catch(IOException e) {
                e.printStackTrace();
            }catch(Exception e) {
                e.printStackTrace();
            }
         }

推荐阅读