每次创建一个新文件并从中读取

时间:2016-04-25 01:47:10

标签: java file-io

所以我试图将用户输入的新书存入外部文本文件,其中包括书名,作者和ISBN。

我已经成功制作了一个文件。但是,每次重新运行程序以创建新书时,它都说文件已经存在。如何使程序添加到现有文件?或者我应该每次只创建一个新文件?

提前致谢。

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();


                    }

                }

0 个答案:

没有答案