java - Creating a new file each time and reading from it -
so i'm trying store new book user enters external text file includes title of book, author, , isbn.
i have achieved making file. however, each time rerun program create new book, says file exists. how can make program add existing file? or should make make new file each time?
thanks in advance.
public class testmylibrary { //declare global var //create 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 read file static scanner input=new scanner(system.in); static int choice; //user inputs choice keyboard //main method public static void main(string[] args) throws filenotfoundexception { //3 ways create books , add them library //first way book book1=new book("the lord of 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 title of book"); string til=input.next(); system.out.println("enter name of author"); string auth=input.next(); system.out.println("enter isbn of book"); int isbn=input.nextint(); lib.addbook(new book(til,auth,isbn)); //add information file /users/adambunch/documents writefile(myfile); readfile(myfile); //borrow book "the lord of rings" lib.borrowbook("the lord of rings"); //create menu system.out.println(">########################################################################"); system.out.println("> choose 1 of options below typing corresponding number:"); system.out.println(">===================================================================="); system.out.println("1- check library"); system.out.println("2- return book library."); system.out.println("3- borrow book"); system.out.println(">########################################################################"); system.out.println(">enter option here: "); choice=input.nextint(); //user inputs choice if (choice==1){ system.out.println(lib.getbooklist()); } if (choice==2){ system.out.print("enter book title return"); string til1r=input.next(); lib.returnbook(til1r); } if (choice==3){ system.out.print("enter book title borrow"); string til1r=input.next(); lib.borrowbook(til1r); } } //************************************************ //method write file static void writefile(file file)throws filenotfoundexception { if (myfile.exists()){ system.out.println("the file 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 read 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(); } }
Comments
Post a Comment