How to create fixed format file using FixedFormat4j Java Library?

1.7k views Asked by At

I am able to load files in Fixed Format but unable to write a fixed format file using 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

1

There are 1 answers

0
Joman68 On

The export method returns a string representing the marshalled record.

In your code you would need to write out the result of the export command to a FileWriter. So:

before the while loop:

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

after the while loop:

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