[英]How to print only the header into a string in Jackson CSVMapper
I am using Jackson CSVMapper to read a tab separated text file into a list of java objects, and write it back into another tab separated text file.我正在使用 Jackson CSVMapper 将制表符分隔的文本文件读入 Java 对象列表,然后将其写回到另一个制表符分隔的文本文件中。 My class looks like this:我的课是这样的:
@Getter
@Setter
@ToString
@NoArgsConstructor
@JsonPropertyOrder({
"Id",
"Name",
"Address 1",
"Address 2"})
public class InputSchema {
@JsonProperty("Id")private String id;
@JsonProperty("Name") private String name;
@JsonProperty("Address 1") private String address1;
@JsonProperty("Address 2") private String address2;
}
Parser code:解析器代码:
public List<InputSchema> parseAddresses(String fileContent) throws IOException {
List<InputSchema> inputAddresses = new ArrayList<>();
InputStream inputStream = new ByteArrayInputStream(fileContent.getBytes());
MappingIterator<InputSchema> iterator = getInputSchemaMappingIterator(inputStream);
while (iterator.hasNextValue()) {
InputSchema inputAddress = iterator.nextValue();
log.info(inputAddress.toString());
inputAddresses.add(inputAddress);
}
return inputAddresses;
}
private MappingIterator<InputSchema> getInputSchemaMappingIterator(InputStream inputStream) throws IOException {
CsvMapper mapper = new CsvMapper();
mapper.configure(CsvParser.Feature.FAIL_ON_MISSING_COLUMNS, true);
mapper.configure(CsvParser.Feature.TRIM_SPACES, true);
CsvSchema schema = mapper.schemaFor(InputSchema.class)
.withColumnSeparator('\t')
.withHeader()
.withStrictHeaders(true);
ObjectReader reader = mapper.readerFor(InputSchema.class).with(schema);
MappingIterator<InputSchema> iterator = reader.readValues(inputStream);
return iterator;
}
Writer code:编写器代码:
private String toTxt(List<InputSchema> inputAddresses) throws JsonProcessingException {
CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(InputSchema.class)
.withColumnSeparator('\t')
.withLineSeparator("\n")
.withHeader()
.withoutQuoteChar();
return mapper.writer(schema).writeValueAsString(inputAddresses);
}
I want to extract just the header irrespective of the data.无论数据如何,我只想提取标题。 So,所以,
String header = extractHeader();
should return应该回来
"Id\tName\tAddress 1\tAddress 2"
How can I do it?我该怎么做?
Thanks!谢谢!
The following worked: 以下工作:
public static String displayHeader() {
CsvMapper mapper = new CsvMapper();
JavaType javaType = mapper.getTypeFactory().constructType(InputSchema.class);
BeanDescription beanDescription = mapper.getSerializationConfig().introspect(javaType);
List<BeanPropertyDefinition> properties = beanDescription.findProperties();
List<String> columnNames = properties.stream()
.map(e -> e.getName())
.collect(Collectors.toList());
String header = String.join("\t", columnNames);
return header;
}
reference: @cassiomolin 's answer 参考: @cassiomolin的回答
this works for me.这对我有用。
public String extractHeader(CsvSchema schema) throws JsonProcessingException {
CsvSchema headerSchema = schema.withHeader();
return new CsvMapper().writer(headerSchema).writeValueAsString(null);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.