【问题标题】:Convert Java Object to TSV in java在java中将Java对象转换为TSV
【发布时间】:2022-09-27 09:24:25
【问题描述】:

我生成一个报告,我从 postgresql 读取数据,然后填充一个 Java 对象 下面是一种示例骨架,我想在其中显示 Main 对象中有许多集合对象,即 ReportMessageStructure

public class ReportMessageStructure {
    
       
    protected MessageHeader messageHeader;
    
    protected MessageDuration messageDuration;
    
    protected ObjectA sampleListA;
    
    protected ObjectB sampleListB;
    
    protected ObjectC smapleListC;

   }

现在我的要求是将数据写入 TSV 文件。你能否建议我最好的方法来做到这一点。我知道如果必须转换为 XML,我可以使用 JAXB。但是需要转换成 TSV 的方法。任何提示/建议都会有很大帮助。

从对象到 TSV,数据将产生的最大大小约为 700MB

  • 这不是代码编写服务。在提出问题之前,您应该自己尝试一下。

标签: java


【解决方案1】:

您可以使用Apache Commons CSV 库中的CSVPrinterCSVFormat 等于TDF

try (CSVPrinter printer = new CSVPrinter(new FileWriter("csv.txt"), CSVFormat.TDF)) {
     printer.printRecord("id", "userName", "firstName", "lastName", "birthday");
     printer.printRecord(1, "john73", "John", "Doe", LocalDate.of(1973, 9, 15));
     printer.println();
     printer.printRecord(2, "mary", "Mary", "Meyer", LocalDate.of(1985, 3, 29));
 } catch (IOException ex) {
     ex.printStackTrace();
 }

【讨论】:

  • 我喜欢这种方法,非常感谢 Alex。我喜欢这种方法,因为这也可以一次性编写可迭代的。我会试试这个并将我的结果发布在这里。谢谢
猜你喜欢
  • 1970-01-01
  • 2017-04-29
  • 1970-01-01
  • 2016-06-06
  • 1970-01-01
  • 2012-08-01
  • 2012-08-29
  • 2012-05-29
  • 2016-05-14
相关资源
最近更新 更多