标签: apache-commons-csv

如何指示 CSVPrinter 在添加记录时不写标题行?

我有一个 CSV 文件,它是由我正在编写的程序以外的程序创建的,因此我无法更改标题。该文件的标题是ControllerID,Event,Number,ReaderID,Timestamp,TransactionID,. 我已经为CSVPrinter我用来在同一个 CSV 中插入一行的指定了相同的标题。但是,当打印机添加一条记录时,它会在新记录之前添加一个标题行,如下所示:

0,1,2688229830,3,2018-09-03 13:54,63
ControllerID,Event,Number,ReaderID,Timestamp,TransactionID
0,1,4900920,2,2018-09-03,15:15,176492
Run Code Online (Sandbox Code Playgroud)

有问题的代码如下所示:

/* Add an entry to the TransactionLog CSV */
private static boolean logTransaction (
  int cid, int rid, boolean freeExit, long tsMS, long accNum, String reason
) {
  boolean added = false;
  int evt = freeExit ? 8 : 1;
  File transactLog = new File(Locations.getDatabaseDir(), "TransactLog.csv"),

  CSVFormat shortFormat = CSVFormat.DEFAULT.withQuote(null)
    .withAllowMissingColumnNames(true)/*.withTrailingDelimiter(true)*/
    .withHeader(Transaction.getHeader(false));
  // Attempt to read the last transaction ID and add 1 to it, …
Run Code Online (Sandbox Code Playgroud)

java-7 apache-commons-csv

3
推荐指数
1
解决办法
2679
查看次数

如何在java中使用Apache commons csv api更新csv文件中的记录?

我有一个使用 Apache commons API 编写的 csv 文件,我也可以读取该文件,但是我无法知道如何使用 Apache commons API 编辑 csv 文件中的记录值,需要这方面的帮助。

java apache-commons-csv

2
推荐指数
1
解决办法
7038
查看次数

Apache Commons CSV 解析器:无法读取值

我正在使用 apache commons CSV 解析器将 CSV 转换为地图。在地图中,我无法通过 intellij 调试器读取某些值。如果我手动输入map.get(“key”),则该值为空。但是,如果我从地图上复制粘贴密钥,我就会获取数据。无法理解出了什么问题。任何指示都会有所帮助。谢谢

这是我的 CSV 解析器代码:

 private CSVParser parseCSV(InputStream inputStream) {
        System.out.println("What is the encoding "+ new InputStreamReader(inputStream).getEncoding());
        try {
            return new CSVParser(new InputStreamReader(inputStream), CSVFormat.DEFAULT
                    .withFirstRecordAsHeader()
                    .withIgnoreHeaderCase()
                    .withSkipHeaderRecord()
                    .withTrim());
        } catch (IOException e) {
            throw new IPRSException(e);
        }
    }
Run Code Online (Sandbox Code Playgroud)

java-8 apache-commons-csv

2
推荐指数
1
解决办法
2584
查看次数

如何使用 Apache Common CSV CSVPrinter 添加新行?

我有一个包含使用Apache Common CSV library 1.5生成 CSV 文件的方法的类

public class CSVGenerator {

    private static final String CSV_FILE = "./credentials.csv";
    private static CSVPrinter csvPrinter;

    public static void generateCSV(String FirstName, String LastName, String DOB) {


        try {
            BufferedWriter writer = Files.newBufferedWriter(Paths.get(CSV_FILE) );

            csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT
                    .withHeader("FirstName", "LastName", "DOB"));

            csvPrinter.printRecord(FirstName, LastName, DOB);
            csvPrinter.flush();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的主类中有一个方法,这个方法会调用该方法generateCSV()几次。
如何写入新行并将其附加到现有 CSV 文件中?使用我当前的实现,它将继续覆盖我的第一行。

更直接的解决方案是在任何 Java 集合(数组或列表)中收集我的所有数据,然后在最后迭代集合并将其一次性写入 CSV。但我不会走那条路。我更喜欢将一行写入 CSV ,做其他事情,然后再次调用该方法以写入新行并将其附加到现有的 CSV 中。

谢谢。

java apache-commons-csv

2
推荐指数
1
解决办法
4383
查看次数

Java:使用不带行号的 InputStream 和 Apache Commons CSV

这可能非常简单,但我还没有找到执行此操作的选项。我正在尝试使用 Apache Commons CSV 读取文件以供以后验证。有问题的 CSV 作为输入流提交,它似乎在读取文件时向文件添加了一个附加列,其中包含行号。如果可能的话,我希望能够忽略它,因为标题行不包含数字,这会导致错误。InputStream 中是否已有选项可以执行此操作,或者我必须设置某种后处理?

我使用的代码如下:

public String validateFile(InputStream filePath) throws Exception{
        System.out.println("Sending file to reader");
        System.out.println(filePath);
        InputStreamReader in = new InputStreamReader(filePath);
        //CSVFormat parse needs a reader object
        System.out.println("sending reader to CSV parse");
        for (CSVRecord record : CSVFormat.DEFAULT.withHeader().parse(in)) {
            for (String field : record) {
                System.out.print("\"" + field + "\", ");
            }
            System.out.println();
        }
        return null;
    }
Run Code Online (Sandbox Code Playgroud)

使用时withHeader(),我最终出现以下错误:

java.lang.IllegalArgumentException: A header name is missing in [, Employee_ID, Department, Email]
Run Code Online (Sandbox Code Playgroud)

我不能简单地跳过它,因为我需要对标题行进行一些验证。

另外,这里还有一个 CSV 文件示例:

"Employee_ID", "Department", …
Run Code Online (Sandbox Code Playgroud)

java csv inputstream apache-commons-csv

0
推荐指数
1
解决办法
5966
查看次数

Java CSVParser读取后变空

在下面的代码片段中,我尝试使用 Apache Commons 库中的 CSVParser 读取 Excel 文件。问题是为什么records.getRecords();列表为records空。我应该如何意识到这种行为?

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class ReadCSV {

    public ReadCSV() {
    }

    /* Define headers as enum */
    enum HEADER {
        ID, NAME, AGE
    }

    public List<List<String>> ReadCSVToList(String csvPath) throws IOException, HighBalanceException {
        List<List<String>> csvList = new ArrayList<>();
        try {


            Reader reader = new FileReader(csvPath);
            CSVParser  records = CSVFormat.DEFAULT.withHeader(HEADER.class).parse(reader);
            List<CSVRecord> records1 = …
Run Code Online (Sandbox Code Playgroud)

java apache-commons-csv

-2
推荐指数
2
解决办法
1341
查看次数

标签 统计

apache-commons-csv ×6

java ×4

csv ×1

inputstream ×1

java-7 ×1

java-8 ×1