Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BookshelfFilestore {
- private static final String DEFAULT_SHELF_FILE = "shelf.txt";
- public static void main( String[] args ) throws IOException
- {
- File shelfFile = new File(DEFAULT_SHELF_FILE);
- PrintWriter writer = new PrintWriter(DEFAULT_SHELF_FILE);
- Bookshelf shelf = new Bookshelf();
- shelf.addBook(new Book("Pizza Cookbook", 751, "Pizza Chef", "Non-fiction"));
- shelf.addBook(new Book("Space Cowboys", 152, "Somebody Cool", "Sci-Fi"));
- shelf.addBook(new Book("Space Cadets", 852, "Somebody Cool", "Sci-Fi"));
- shelf.addBook(new Book("Cable News", 659, "Johny", "Non-fiction"));
- shelf.addBook(new Book("Pizza Cookbook", 751, "Pizza Chef", "Non-fiction"));
- for (Book book : shelf.getBookshelf()) {
- writer.write(book.getTitle() + "\n");
- writer.write(book.getPages() + "\n");
- writer.write(book.getAuthor() + "\n");
- writer.write(book.getGenre() + "\n");
- writer.write("\n");
- writer.flush();
- }
- Bookshelf shelf2 = new Bookshelf();
- Scanner fileReader = new Scanner(shelfFile);
- fileReader.useDelimiter("\n\n");
- while (fileReader.hasNext()) {
- String bookStr = fileReader.next();
- Scanner bookReader = new Scanner(bookStr);
- String title, author, genre;
- int pages;
- // For each book in the file's list
- for (int i = 0; bookReader.hasNext(); i++) {
- title = bookReader.nextLine();
- pages = Integer.parseInt(bookReader.nextLine());
- author = bookReader.nextLine();
- genre = bookReader.nextLine();
- shelf2.addBook(new Book(title, pages, author, genre));
- }
- }
- for (Book book : shelf2.getBookshelf()) {
- System.out.println(book.displayDetails() + "\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment