簡體   English   中英

如何在 Java 中讀取多行值 CSV 文件?

[英]How to read multiple line value CSV files in Java?

由於一個有效的 JSON 文件可能只包含一個 JSON,我打算將多個 JSON 放入一個以“;”分隔的 CSV 文件中。 並使用 OpenCSV 讀取它。 (這是為了將那些 JSON 數據放在文件中,而不是 JUnit 類,以使其更具可讀性。)

有沒有辦法讀取行直到分隔符“;” 發生? 也許通過附加字段_name增強 JSON 是一個好主意,該字段可以用作 Map 將 JSON 讀入的鍵!?? 我首先嘗試以這種方式讀入列表,但它將所有行讀入列表,只是消除了“;”。 我需要一種閱讀方式,直到“;” 發生時,將之前讀取的所有行存儲到 Map<String, String> 或更好的 Map<String, JsonNode> 中,然后繼續下一個。

    final ClassLoader classLoader = CrmServiceUT.class.getClassLoader();
    final File file = new File(classLoader.getResource("data/UpdatedDeal1.csv").getFile());
    final List<List<String>> records = new ArrayList<List<String>>();
    final CSVParser parser = new CSVParserBuilder().withSeparator(';').build();
    try (final BufferedReader br = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8);
         final CSVReader csvReader = new CSVReaderBuilder(br).withCSVParser(parser).build()){
        String[] values = null;
                List<String> list = null;
        while ((values = csvReader.readNext()) != null) {
            if( !Stream.of(values).anyMatch(x -> x.contains(";")) ) {
                list = Arrays.asList(values);
            }
            if(list.contains(";")) {
                list.remove(";");
            }
                records.add(list);
        }
    } catch (CsvValidationException e) {
        e.printStackTrace();
    } catch (CsvException e) {
        e.printStackTrace();
    }

JSON 可能完全不同,有幾行或很多行:

{
  "_nodeName": "updatedDeal1",
  "results": [
    184896670
  ],
  "hasMore": false,
  "offset": 184896670
}
;
{
"_nodeName": "updatedDeal1",
"results": [
184896670
],
"hasMore": false,
"abcde": "xyz",
"offset": 184896670
}

有了 LHCHIN 的回答,我最終在這個解決方案中額外處理了分隔符 (';') 可能跟在一行中的下一個 JSON 的情況:

@BeforeAll
public static void beforeAll() throws IOException {
    final ClassLoader classLoader = CrmServiceUT.class.getClassLoader();
    final File file = new File(classLoader.getResource("data/jsonsUT.csv").getFile());
    final StringBuilder sb = new StringBuilder();
    for (final String line : Files.readAllLines(file.toPath(), StandardCharsets.UTF_8)) {
        sb.append(line);
        if (line.endsWith(";")) {
            putJsonToMap(sb.substring(0, sb.length()-1));
            sb.delete(0, sb.length());
        }
    }
    putJsonToMap(sb.substring(0, sb.length()));
}

/**
 * Fills a map with JSON used throughout the unit tests.
 *
 * @param jsonStr       The JSON to be stored.
 * @throws IOException
 */
private static void putJsonToMap(final String jsonStr) throws IOException {
    final JsonNode node = mapper.readTree(jsonStr);
    jsons.put(node.get("_nodeName").asText(), node);
    ((ObjectNode) node).remove("_nodeName");
}

對於您的情況,我看不到使用opencsv任何優勢,那么為什么不自己處理呢!

以下示例顯示了如何逐行讀取給定的 CSV 文件並將每一行放入StringBuilder進行連接,然后一旦找到以;結尾的行; ,將StringBuilder的內容添加到String的列表中。

代碼片段

List<String> records = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (String line : Files.readAllLines(file.toPath(), StandardCharsets.UTF_8)) {
    sb.append(line);
    if (line.endsWith(";")) {
        records.add(sb.substring(0, sb.length()-1));
        sb.delete(0, sb.length());
    }
}

之后, List中的每個元素都是一個 JSON 字符串,您可以直接訪問它們以進行進一步操作。

您可以像這樣讀取 CSV 文件,並可以獲取每一行和每一列

BufferedReader csvReader;
csvReader = new BufferedReader(new FileReader(file.getPath())); // csv file path
while ((row = csvReader.readLine()) != null) {
String[] data = row.split(",");  // in data you have each column value for each row
}

我想,就去做吧。

json.replace('\n', '').replace(';','\n');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM