Advertisement
EBobkunov

Serializable Books Staff

Mar 11th, 2023
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.65 KB | Source Code | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.     public static void main(String[] args) {
  6.         ReaderLibrary library = new ReaderLibrary();
  7.         try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("serialized.dat")))
  8.         {
  9.             Book book = new Book("J.R.R. Tolkien", "The Lord of the Rings", 1954, 1216, 1000);
  10.             Book book2 = new Book("George Orwell", "1984", 1949, 328, 500);
  11.             Book book3 = new Book("J.R.R. Tolkien", "The Hobbit", 1937, 320, 500);
  12.             List<Book> books = new ArrayList<>();
  13.             books.add(book);
  14.             books.add(book2);
  15.             books.add(book3);
  16.             library.addMyReadBooks(books, "serialized.dat");
  17.             library.getMySerializedBooks("serialized.dat");
  18.             //library.displayMyBooks(books);
  19.         }
  20.         catch(Exception ex){
  21.  
  22.             System.out.println(ex.getMessage());
  23.         }
  24.  
  25.  
  26.     }
  27.     static class Book implements Serializable{
  28.         private static final long serialVersionUID = 1L;
  29.         public String author;
  30.         public String title;
  31.         public int issueYear;
  32.         public int pageNumber;
  33.         public int bookmark;
  34.  
  35.         public String getAuthor() {
  36.             return author;
  37.         }
  38.  
  39.         public void setAuthor(String author) {
  40.             this.author = author;
  41.         }
  42.  
  43.         public String getTitle() {
  44.             return title;
  45.         }
  46.  
  47.         public void setTitle(String title) {
  48.             this.title = title;
  49.         }
  50.  
  51.         public int getIssueYear() {
  52.             return issueYear;
  53.         }
  54.  
  55.         public void setIssueYear(int issueYear) {
  56.             this.issueYear = issueYear;
  57.         }
  58.  
  59.         public int getPageNumber() {
  60.             return pageNumber;
  61.         }
  62.  
  63.         public void setPageNumber(int pageNumber) {
  64.             this.pageNumber = pageNumber;
  65.         }
  66.  
  67.         public int getBookmark() {
  68.             return bookmark;
  69.         }
  70.  
  71.         public void setBookmark(int bookmark) {
  72.             this.bookmark = bookmark;
  73.         }
  74.  
  75.         public Book(String author, String title, int issueYear, int pageNumber, int bookmark) {
  76.             this.author = author;
  77.             this.title = title;
  78.             this.issueYear = issueYear;
  79.             this.pageNumber = pageNumber;
  80.             this.bookmark = bookmark;
  81.         }
  82.     }
  83.     static class ReaderLibrary{
  84.         public ArrayList<Book> books = new ArrayList<>();
  85.         public void addMyReadBooks(List<Book> books, String serializableFilePath){
  86.             try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(serializableFilePath)))
  87.             {
  88.                 for (Book book : books) {
  89.                     oos.writeObject(book);
  90.                 }
  91.             }
  92.             catch(Exception ex){
  93.  
  94.                 System.out.println(ex.getMessage());
  95.             }
  96.         }
  97.         public void getMySerializedBooks(String serializableFilePath){
  98.             try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(serializableFilePath)))
  99.             {
  100.                 ReaderLibrary readerLibrary = (ReaderLibrary) ois.readObject();
  101.                 this.books = readerLibrary.books;
  102.                 System.out.println(this.books);
  103.             }
  104.             catch(Exception ex){
  105.                 System.out.println(ex.getMessage());
  106.             }
  107.         }
  108.         public void displayMyBooks(List<Book> books){
  109.             for(Book book : books){
  110.                 System.out.println(book.author + " " + book.title + " " + book.issueYear + " " + book.pageNumber + " " + book.bookmark);
  111.             }
  112.         }
  113.     }
  114.  
  115. }
Tags: Serializable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement