1

I am using JSEFA to serialize my java object to into single row in CSV using JSEFA. I have arround 46 fields in one single class ,

But now the problem is i want to split this class into different sub classes which holds information,One class contains fields of CSV for first 23 positions and next class contains remaining position. When i tried approach of Prefix it is inserting data into two different rows but i need all data of two classes into a single row can some body suggest best approach using JSEFA or any other .

First class contains first two fields:

@CsvDataType(defaultPrefix = "1")

public class TestClass1 implements Serializable {

private static final long serialVersionUID = 371968159365574089L;

@CsvField(pos = 1)
private int id;

@CsvField(pos = 2)
private String Name;

public int getId() {
    return this.id;
}

public void setId(final int id) {
    this.id = id;
}

public String getName() {
    return this.Name;
}

public void setName(final String name) {
    this.Name = name;
}
  }

Sub Class 2 which contains remaining two fields:

@CsvDataType(defaultPrefix = "2")
 public class TestClass2 implements Serializable {

private static final long serialVersionUID = 371968159365574089L;

@CsvField(pos = 3)
private int orderno;

@CsvField(pos = 4)
private String orderName;

public int getOrderno() {
    return this.orderno;
}

public void setOrderno(final int orderno) {
    this.orderno = orderno;
}


public String getOrderName() {
    return orderName;
}


public void setOrderName(String orderName) {
    this.orderName = orderName;
}


  }

main method for combining:

 public static void main(final String[] args) {

final File file = new File("/home/chandra/Documents/dataExcelFiles/final.csv");
    file.getParentFile().mkdirs();
 Serializer serializer = CsvIOFactory.createFactory(TestClass.class,  TestClass2.class).createSerializer();

    TestClass prod1 = new TestClass();

    prod1.setId(1);
    prod1.setName("HTC");

    TestClass2 prod2 = new TestClass2();
    prod2.setOrderno(512);
    prod2.setOrderName("HTC500");

    try {

        serializer.open(new FileWriter(file));
        serializer.write(prod1);
        serializer.write(prod2);
        serializer.close(true);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Using this approach out put is like this :

  • 1 1 HTC
  • 2 512 HTC500

but i need out put every thing in one row like this:

  • 1 HTC 512 HTC500

Thanks, Chandra

1 Answer 1

1

serializer.write method will write one record. Since you use it twice with (different arguments of jsefa classes), it writes 2 records

What you need is something like this:

Sub Class 1:

@Embeddable
@CsvDataType
public class TestClass1 implements Serializable {

private static final long serialVersionUID = 371968159365574089L;

@CsvField(pos = 1)
private int id;

@CsvField(pos = 2)
private String Name;

// getter setter methods
}

Sub Class 2:

@Embeddable
@CsvDataType
public class TestClass2 implements Serializable {

private static final long serialVersionUID = 371968159365574089L;

@CsvField(pos = 3)
private int orderno;

@CsvField(pos = 4)
private String orderName;

// getter setter methods
}

Parent Class:

@CsvDataType()
public class TestClass {
    @CsvField ( pos = 1)
    private TestClass1 testClass1;
    @CsvField ( pos = 2)
    private TestClass2 testClass2;

    // getter setter methods
}

Your main method:

public static void main(final String[] args) {

final File file = new File("/home/chandra/Documents/dataExcelFiles/final.csv");
    file.getParentFile().mkdirs();
 Serializer serializer = CsvIOFactory.createFactory(TestClass.class,  TestClass2.class).createSerializer();

    TestClass testClass = new TestClass();

    testClass.getTestClass1().setId(1);
    testClass.getTestClass1().setName("HTC");
    testClass.getTestClass2().setOrderno(512);
    testClass.getTestClass2().setOrderName("HTC500");

    try {

        serializer.open(new FileWriter(file));
        serializer.write(testClass);
        serializer.close(true);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

If you want to use Default Prefix, you can add DefaultPrefix as '1' in TestClass and remove 'id' from TestClass1. That means, all records will start with '1'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.