简体   繁体   English

如何使用 FixedFormat4j Java 库创建固定格式文件?

[英]How to create fixed format file using FixedFormat4j Java Library?

I am able to load files in Fixed Format but unable to write a fixed format file using FixedFormat4j.我能够以固定格式加载文件,但无法使用 FixedFormat4j 编写固定格式文件。

Any idea how to do that?知道怎么做吗?

public class MainFormat {
public static void main(String[] args) {
    new MainFormat().start();
}
private FixedFormatManager manager;
public void start(){
    System.out.println("here");
    manager = new FixedFormatManagerImpl();
    try {
        BufferedReader br = new BufferedReader(new FileReader("d:/myrecords.txt"));
        System.out.println("here1");
        String text;
        MyRecord mr = null;
        while ((text = br.readLine()) != null) {
          mr = manager.load(MyRecord.class, text);
            System.out.println(""+mr.getAddress() + " - "+mr.getName());
        }
        mr.setName("Rora");
        manager.export(mr);

    } catch (IOException | FixedFormatException ex) {
        System.out.println(""+ex);
    }
}
}

I have seen export method but don't understand how to use it?我看过导出方法,但不明白如何使用它? Nothing happens in above code上面的代码什么也没发生

The export method returns a string representing the marshalled record. export 方法返回一个表示编组记录的字符串。

In your code you would need to write out the result of the export command to a FileWriter.在您的代码中,您需要将导出命令的结果写出到 FileWriter。 So:所以:

before the while loop:在while循环之前:

FileWriter fw = new FileWriter("d:/myrecords_modified.txt", true);
BufferedWriter bw = new BufferedWriter(fw);

after the while loop:在while循环之后:

mr.setName("Rora");
String modifiedRecord = manager.export(mr);
bw.write(modifiedRecord);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM