1

So I'm trying to store a new book that the user enters into an external text file which includes the title of the book, the author, and the ISBN.

I have successfully achieved making a file. However, each time I rerun the program to create a new book, it says the file already exists. How can I make the program add to the existing file? Or should I just make it make a new file each time?

Thanks in advance.

public class TestMyLibrary {

    //Declare global var
    //Create a file in documents

    static File myFile=new File("/Users/adambunch/Documents/Library.rtf");

    //Create library object
    static Library lib=new Library ("400 Main St.");

    //Scanner to read from the file
    static Scanner input=new Scanner(System.in);
    static int choice; //User inputs a choice from the keyboard

    //Main method

        public static void main(String[] args) throws FileNotFoundException {

            //3 ways to Create books and add them to the Library

            //First way

            Book book1=new Book("The Lord of the Rings", "Sally Smith", 12345);
            lib.addBook(book1);

            //Second way

            lib.addBook(new Book("Harry Potter", "Sara Ahmed", 62347288));
            lib.addBook(new Book("Homes", "Sara Ahmed", 623743227));

            //Third way
            System.out.println("Enter the title of the book");
            String til=input.next();

            System.out.println("Enter the name of the author");
            String auth=input.next();

            System.out.println("Enter the ISBN of the book");
            int isbn=input.nextInt();
            lib.addBook(new Book(til,auth,isbn));

            //Add information into the file /Users/adambunch/Documents

            writeFile(myFile);

            readFile(myFile);

            //Borrow book "The Lord of the Rings"

            lib.borrowBook("The lord of the rings");

            //Create a menu

            System.out.println(">########################################################################");

            System.out.println("> Choose one of the options below by typing the corresponding number:");

            System.out.println(">====================================================================");

            System.out.println("1- Check library");
            System.out.println("2- Return book to the Library.");
            System.out.println("3- Borrow a book");

            System.out.println(">########################################################################");

            System.out.println(">Enter your option here: ");

            choice=input.nextInt(); //User inputs a choice
                if (choice==1){     

                    System.out.println(lib.getBookList());
                }

                if (choice==2){
                    System.out.print("Enter book title to return");

                    String til1r=input.next();
                    lib.returnBook(til1r);
                }

                if (choice==3){

                    System.out.print("Enter book title to borrow");

                    String til1r=input.next();
                    lib.borrowBook(til1r);
                }
        }

                //************************************************
                //Method to write into the file

                static  void writeFile(File file)throws FileNotFoundException {

                    if (myFile.exists()){

                        System.out.println("The file already exists");

                        System.exit(0);

                    }

                    PrintWriter output=new PrintWriter(myFile);
                    output.println(lib.address);
                    for(Book book:lib.bookList)
                        output.println(book.toString());
                    output.close();

                }

                //************************************************
                //Method to read from the file

                    static void readFile(File file)throws FileNotFoundException{
                        Scanner input=new Scanner(myFile);
                        String line="";
                        while(input.hasNext()) {
                            line=line+input.nextLine()+"\n";

                        }

                        System.out.println(line);
                        input.close();


                    }

                }
randominstanceOfLivingThing
  • 16,873
  • 13
  • 49
  • 72
Adam Bunch
  • 113
  • 1
  • 2
  • 14
  • You check to see if `myFile` exists, but your `writeFile` passes a `File` reference ... which one are you meant to be using? – MadProgrammer Apr 25 '16 at 01:50
  • To append text, http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java – J.Cheong Apr 25 '16 at 01:53
  • I'm meant to be using myFile, sorry. You're talking about static void writeFile(File file) correct? It should be static void writeFile(File myFile)? – Adam Bunch Apr 25 '16 at 01:56
  • Should I just get rid of the method I created and just add it to the main method? – Adam Bunch Apr 25 '16 at 02:08

0 Answers0