Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- ReaderLibrary library = new ReaderLibrary();
- try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("serialized.dat")))
- {
- Book book = new Book("J.R.R. Tolkien", "The Lord of the Rings", 1954, 1216, 1000);
- Book book2 = new Book("George Orwell", "1984", 1949, 328, 500);
- Book book3 = new Book("J.R.R. Tolkien", "The Hobbit", 1937, 320, 500);
- List<Book> books = new ArrayList<>();
- books.add(book);
- books.add(book2);
- books.add(book3);
- library.addMyReadBooks(books, "serialized.dat");
- library.getMySerializedBooks("serialized.dat");
- //library.displayMyBooks(books);
- }
- catch(Exception ex){
- System.out.println(ex.getMessage());
- }
- }
- static class Book implements Serializable{
- private static final long serialVersionUID = 1L;
- public String author;
- public String title;
- public int issueYear;
- public int pageNumber;
- public int bookmark;
- public String getAuthor() {
- return author;
- }
- public void setAuthor(String author) {
- this.author = author;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public int getIssueYear() {
- return issueYear;
- }
- public void setIssueYear(int issueYear) {
- this.issueYear = issueYear;
- }
- public int getPageNumber() {
- return pageNumber;
- }
- public void setPageNumber(int pageNumber) {
- this.pageNumber = pageNumber;
- }
- public int getBookmark() {
- return bookmark;
- }
- public void setBookmark(int bookmark) {
- this.bookmark = bookmark;
- }
- public Book(String author, String title, int issueYear, int pageNumber, int bookmark) {
- this.author = author;
- this.title = title;
- this.issueYear = issueYear;
- this.pageNumber = pageNumber;
- this.bookmark = bookmark;
- }
- }
- static class ReaderLibrary{
- public ArrayList<Book> books = new ArrayList<>();
- public void addMyReadBooks(List<Book> books, String serializableFilePath){
- try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(serializableFilePath)))
- {
- for (Book book : books) {
- oos.writeObject(book);
- }
- }
- catch(Exception ex){
- System.out.println(ex.getMessage());
- }
- }
- public void getMySerializedBooks(String serializableFilePath){
- try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(serializableFilePath)))
- {
- ReaderLibrary readerLibrary = (ReaderLibrary) ois.readObject();
- this.books = readerLibrary.books;
- System.out.println(this.books);
- }
- catch(Exception ex){
- System.out.println(ex.getMessage());
- }
- }
- public void displayMyBooks(List<Book> books){
- for(Book book : books){
- System.out.println(book.author + " " + book.title + " " + book.issueYear + " " + book.pageNumber + " " + book.bookmark);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement