CSVWriter在每行之间添加了空行。

huangapple go评论84阅读模式
英文:

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&lt;Ref&gt; branches = git.branchList().call();
        for (Ref ref : branches)
        {

            logger.info(ref.getName());

        }

        Iterable&lt;RevCommit&gt; commits = git.log().all().call();

        for (RevCommit revCommit : commits) { //itero tutti i commit.
            
            String pattern = &quot;MM/dd/yyyy HH:mm:ss&quot;;
            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

huangapple
  • 本文由 发表于 2020年4月4日 23:38:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61030539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定