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

Java 每次创建一个新文件并从中读取,java,file-io,Java,File Io,因此,我试图存储一本新书,用户将其输入一个外部文本文件,其中包括书名、作者和ISBN 我已经成功地实现了制作文件。但是,每次我重新运行程序创建新书时,它都会说文件已经存在。如何使程序添加到现有文件中?还是我应该让它每次都生成一个新文件 提前谢谢 public class TestMyLibrary { //Declare global var //Create a file in documents static File myFile=new File("/User

因此,我试图存储一本新书,用户将其输入一个外部文本文件,其中包括书名、作者和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();


                    }

                }

您检查
myFile
是否存在,但您的
writeFile
传递了一个
文件
引用。。。您打算使用哪一个?要添加文本,我打算使用myFile,抱歉。你说的是静态void writeFile(文件),对吗?它应该是静态的void writeFile(File myFile)?我应该去掉我创建的方法并将其添加到主方法中吗?您可以检查
myFile
是否存在,但是您的
writeFile
传递了一个
文件
引用。。。您打算使用哪一个?要添加文本,我打算使用myFile,抱歉。你说的是静态void writeFile(文件),对吗?它应该是静态的void writeFile(File myFile)?我是否应该放弃我创建的方法并将其添加到主方法中?