-5

I am trying to write an object(Transaction) in text file. The Transaction object contains card no, amount, date. I need to write the object in text file as below with some gaps.

 Card no      Amount      Date

  12335       900.00     29/09/2010

I have used ObjectOutputStream to write the object in the file. But I couldn't able to give the gaps in this case.

How to write the transaction object in a file with some gaps, so that it can be aligned with header?

Any help is appreciated

Vegard
  • 4,802
  • 1
  • 20
  • 30
Ramya
  • 1,067
  • 6
  • 16
  • 29
  • 6
    Where's the code for what you've attempted? – nhgrif Oct 01 '13 at 03:50
  • You can't serialise an object in this way, you need to write the "text" represented by the text to a text file, using maybe something like `String#format` – MadProgrammer Oct 01 '13 at 04:08
  • Deserialization will give you the same object you serialized. Read this post http://stackoverflow.com/questions/11653374/whats-the-difference-between-serialization-and-simply-store-the-object-on-disk – Sunil Manheri Oct 01 '13 at 04:13

3 Answers3

0

Why are you concerned about gaps in ObjectOutputStream. Its mainly used for writing to file and recreating objects back. If you want to use it to write it to a file in a human readable format suggest you to use normal file output stream and not object output stream.

Akhil K Nambiar
  • 3,835
  • 13
  • 47
  • 85
  • Thank you for your quick reply. In normal file output stream how to provide gaps and write it in a file at single stretch? – Ramya Oct 01 '13 at 03:56
  • @Ramya hope this link helps http://www.tutorialspoint.com/java/java_filewriter_class.htm – Akhil K Nambiar Oct 01 '13 at 03:59
  • Using filewriter I was able to write the object with out gaps. How to specify the position to write the object in this position. – Ramya Oct 01 '13 at 04:09
0

I have no idea what your Transaction object looks like. But this should provide some clues. Note System.out.printf(). You can substitute your own output streams. I threw in extra stuff on the Calendar object just so you can get a sense of it...

private class Transaction {
    public int cardno;
    public BigDecimal amt;
    public Date someDate;
}

public void zz2() {
    Transaction t = new Transaction();
    t.cardno = 12335;
    t.amt = new BigDecimal("900.00");

    Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal.set( Calendar.YEAR,        2013 );
    cal.set( Calendar.MONTH,       9);  // zero offset so 9 = Oct              
    cal.set( Calendar.DATE,        1 ); // NOT zero offset...                  
    cal.set( Calendar.HOUR_OF_DAY, (15) );  // 3PM                             
    cal.set( Calendar.MINUTE,      30 );
    cal.set( Calendar.SECOND,      44 );
    cal.set( Calendar.MILLISECOND, 700 );

    t.someDate = cal.getTime();

    DateFormat df = new SimpleDateFormat("MM/dd/yyyy");

    String s = df.format(t.someDate);

    System.out.println("Card no      Amount      Date");
    System.out.printf ("%6d %12s   %10s\n",
                      t.cardno,
                      t.amt,
                      s);
}
Buzz Moschetti
  • 7,057
  • 3
  • 23
  • 33
0

Maybe I'm missing something, but have you tried using tabs (escaped in a string to "\t") in the positions you need the gaps?

System.out.println("Card no\tAmount\tDate");

Similarily for the println statement where you're outputting the values.

Vegard
  • 4,802
  • 1
  • 20
  • 30