如何在Java中读取多行值CSV文件?
由于一个有效的JSON文件可能只包含一个JSON,我打算将多个JSON放入一个CSV文件中,并使用OpenCSV读取它。(用于将这些JSON数据放在JUnit类之外的文件中,以使其可读性更好。) 有没有办法读取多行,直到出现分隔符“;”为止?也许通过增加一个字段名来增强JSON是个好主意,该字段名可以用作映射的键,以便将JSON读入!?? 我先用这种方法读取列表,但它会读取列表中的所有行,只是删除了“;”。我需要一种直到出现“;”的读取方法,将之前读取的所有行存储到地图或更好的地图中,然后继续下一个如何在Java中读取多行值CSV文件?,java,json,csv,opencsv,Java,Json,Csv,Opencsv,由于一个有效的JSON文件可能只包含一个JSON,我打算将多个JSON放入一个CSV文件中,并使用OpenCSV读取它。(用于将这些JSON数据放在JUnit类之外的文件中,以使其可读性更好。) 有没有办法读取多行,直到出现分隔符“;”为止?也许通过增加一个字段名来增强JSON是个好主意,该字段名可以用作映射的键,以便将JSON读入!?? 我先用这种方法读取列表,但它会读取列表中的所有行,只是删除了“;”。我需要一种直到出现“;”的读取方法,将之前读取的所有行存储到地图或更好的地图中,然后继续下
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();
}
根据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");
}
您可以像这样读取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');
对于您的情况,我看不出使用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 records=new ArrayList();
StringBuilder sb=新的StringBuilder();
for(字符串行:Files.readAllLines(file.toPath(),StandardCharsets.UTF_8)){
某人附加(行);
if(第行结束(“;”)){
添加(sb子字符串(0,sb长度()-1));
sb.删除(0,sb.length());
}
}
之后,列表中的每个元素都是一个JSON字符串,您可以直接访问它们进行进一步操作。它是什么语言,Java?请添加一个标签。这看起来很麻烦。JSON和CSV不能很好地混合。为什么不将JSON对象放在带有数组的JSON对象中呢?你可以给它一个包含你想读的任何内容的数组的单元素文档。单个文档甚至不需要相似。给我一个示例文件。csvAnd如何处理拆分为多行的JSON文档?你一次只能读一行!我的问题是我不知道如何读取CSV文件。@du它给我一个CSV文件示例。。前-1分
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());
}
}