英文:
CSVWriter adds blank line beetween each row
问题
try(FileWriter fileWriter1 = new FileWriter(csvPath)){
    CSVWriter csvWriter = new CSVWriter(fileWriter1);
    Git git = Git.open(new File(Cpath));
    Repository repository = FileRepositoryBuilder.create(new File(Cpath));
    String repo = String.valueOf(repository);
    logger.info(repo);
    List<Ref> branches = git.branchList().call();
    for (Ref ref : branches) {
        logger.info(ref.getName());
    }
    Iterable<RevCommit> commits = git.log().all().call();
    for (RevCommit revCommit : commits) {
        String pattern = "MM/dd/yyyy HH:mm:ss";
        DateFormat df = new SimpleDateFormat(pattern);
        String date = df.format(revCommit.getAuthorIdent().getWhen());
        String[] columns = {date, revCommit.getFullMessage()};
        csvWriter.writeNext(columns);
    }
    csvWriter.flush();
    csvWriter.close();
}
英文:
I am trying to write some data into a csv file but some of them are written at the end of the line and there is a blank space between each row. I have also tried do collect data into an ArrayList of strings and then use the method csvWriter.writeAll but it gives me the same result.
try(FileWriter fileWriter1 = new FileWriter(csvPath)){
        CSVWriter csvWriter = new CSVWriter(fileWriter1);
        //Impostazione di Git e della repo.
        Git git = Git.open(new File(Cpath));
        Repository repository = FileRepositoryBuilder.create(new File(Cpath));
        String repo = String.valueOf(repository);
        logger.info(repo);
        List<Ref> branches = git.branchList().call();
        for (Ref ref : branches)
        {
            logger.info(ref.getName());
        }
        Iterable<RevCommit> commits = git.log().all().call();
        for (RevCommit revCommit : commits) { //itero tutti i commit.
            
            String pattern = "MM/dd/yyyy HH:mm:ss";
            DateFormat df = new SimpleDateFormat(pattern);
            String date = df.format(revCommit.getAuthorIdent().getWhen());
            String[] columns = {date, revCommit.getFullMessage()};
            csvWriter.writeNext(new String[]{date, revCommit.getFullMessage()});
         
        }
        
        csvWriter.flush();
        csvWriter.close();
    }
答案1
得分: 1
CSV格式在很大程度上遵循RFC 4180。但是,大多数CSV编写程序一贯地使用\r\n(MS/DOS约定)作为文件结尾。
当您在只期望一个单独的\n作为行结尾(Unix约定)的编辑器(或终端)中阅读该文件时,您会错误地看到额外的行,尽管该文件完全正确。
参考:维基百科
英文:
The CSV format loosely follows the RFC 4180. But most cvs writers consistenly use a \r\n (MS/DOS convention) end of file.
When you read that with an editor (or a terminal) that expects only one single \n for an end of line (Unix convention), you wrongly see additional lines when the file is perfectly correct.
Ref: Wikipedia
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论