How to write ArrayList Objects into text file in java -


i'm trying output arraylist text.file, arraylist has 2 object,librarybook , book. how can print 2 objects text.file? there 4 classes related write text.file. help!

class book {     // declare data members     protected int bookid;     protected string bookname;     protected boolean isavailable;     private int borrowerid;     public static int totalnoofbook = 0;     public static int totalnoofavailablebook = 0;      // constructor     private book() {         // initialization         bookname = "";         isavailable = true;         totalnoofavailablebook++;     }      public book(string name) {         this();         // initialization         bookid = totalnoofbook;         bookname = name;         totalnoofbook++;     }      // methods     // borrow book     public void borrow(librarycard card) {         isavailable = false;         borrowerid = card.getcardid();         card.incrementborrowednum();         totalnoofavailablebook--;     }      // print book information     public void printavailablebookinfo() {         if (isavailable) {             system.out.println("----------------------------------------------------------");             system.out.println("                         book                             ");             system.out.println("----------------------------------------------------------");             system.out.println("book id \t: " + bookid);             system.out.println("book name \t: " + bookname);             system.out.println("----------------------------------------------------------\n");         }     } } 

this subclass.

public class librarybook extends book   // subclass {     private string author;     private int year;     public int totalofbook = 0;      public librarybook(string name, string bk_au, int bk_ye) {         super(name);         author = bk_au;         year = bk_ye;     }       public string getauthor() {         return author;     }      public int getyear() {         return year;     }      public void printavailablebookinfo() {         if (isavailable) {             system.out.println("----------------------------------------------------------");             system.out.println("                     librarybook                          ");             system.out.println("----------------------------------------------------------");             system.out.println("book id    \t: " + super.bookid);             system.out.println("book name  \t: " + super.bookname);             system.out.println("author(s)  \t: " + author);             system.out.println("year       \t  " + year);             system.out.println("----------------------------------------------------------\n");         }     } } 

this class creates arraylist .

import java.util.arraylist;  public class bookrecordsystem {     public static void main(string[] args) {         // testing code below: checking class data member         system.out.println("system start: total number of books = " + book.totalnoofbook);         // creating 4 default library books in arraylist here         arraylist<book> booklist = new arraylist<book>(4);         booklist.add(new librarybook("cocoa design patterns",                 "erik buck & donald yacktman", 2009));         booklist.add(new librarybook("lord of files", "william golding", 2013));         booklist.add(new librarybook("animal farm", "george orwell", 1996));         booklist.add(new book("a book life"));          (int = 0; < booklist.size(); i++) {             object obj = booklist.get(i);             if (obj instanceof librarybook) {                 librarybook librarybook = (librarybook) obj;                 librarybook.printavailablebookinfo();             } else {                 book bk = (book) obj;                 bk.printavailablebookinfo();              }         }          system.out.println("system end: total number of books = " + book.totalnoofbook);         system.out.println("end of program.");         bookio.writelbfile("sample", booklist); //pass bookio , write object file      } } 

write data text.file in below class. try write data of library book.but, when try run program. have error"cannot find symbol oos.writeobject("the author :+blist.get(i).author"); know author exist in librarybook class. how can printed subclass data? use type casting that?

import java.io.*; import java.util.*;  public class bookio implements serializable {     public static boolean writelbfile(string ofilestr, arraylist<book> blist) {         system.out.println("start of writelbfile");         if (blist == null) {             return false;         }          try {             file outfile = new file(ofilestr);             fileoutputstream outfilestream = new fileoutputstream(outfile);             objectoutputstream outobjectstream =                     new objectoutputstream(outfilestream);   /*  for(int = 0; < blist.size() ; i++)         if         {              oos.writeobject("the id   :"+blist.get(i).bookid);             oos.writeobject("the name  :"+blist.get(i).bookname);             oos.writeobject("the name  :"+blist.get(i).author);          }         oos.close();         outfilestream.close();       } }*/              outobjectstream.close();             outfilestream.close();         } catch (filenotfoundexception ex) {             system.out.println("error - filenotfoundexception. write book failure.");             return false;         } catch (ioexception ex) {             system.out.println("error - ioexception. write book failure.");             return false;         }         system.out.println("end of writelbfile");         return true;     } } 

the output expect in textfile :

//here output expect in textfile ----------------------------------------------------------                  librarybook                             ---------------------------------------------------------- book id:  :0 book name :coco design patterns author(s) :erik buck & donald yacktman ----------------------------------------------------------  ----------------------------------------------------------                  librarybook                             ---------------------------------------------------------- book id:  :1 book name :lord of files author(s) :william colding ----------------------------------------------------------  ----------------------------------------------------------                  librarybook                             ---------------------------------------------------------- book id:  :2 book name :animal farm author(s) :george orwell year      :1996 ----------------------------------------------------------  ----------------------------------------------------------                   book                            ---------------------------------------------------------- book id:  :3 book name :a book life ---------------------------------------------------------- 

for java version greater 1.7 use printwriter

try(printwriter out = new printwriter(new bufferedwriter(new filewriter(location, true))))   {    out.println(text); }catch (ioexception e) {    system.out.println(e); } 

in example

try(printwriter out = new printwriter(new bufferedwriter(new filewriter(location, true))))   {      for(book b:list)      {          out.println(".........................................");          out.println("            librarybook");          out.println(".........................................");          //add book details      }     }catch (ioexception e) {        system.out.println(e);     } 

Comments

Popular posts from this blog

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -