Question:
I need to direct java txt files to a specific folder
Repl link:
https://replit.com/@Piano12/library?v=1
code snippet
String keyboard = "";
String fileName = "";
String stringi = "";
boolean doesHave = false;
boolean canMake = true;
boolean tooMany = false;
int i = 1;
out.println();
out.println("What is the title of the book?");
String bookName = input.nextLine();
out.println("OK! How many pages does this book have?");
String pageNum = input.nextLine();
out.println("Nice! What is the genera of this book?");
String bookGenera = input.nextLine();
fileName = bookName + " No.1.txt";
while (doesHave == false) {
// checks for the first time if the book exists
File book = new File(fileName);
try {
if (book.createNewFile()) {
out.println("Book added: " + bookName + "!");
doesHave = true;
} else {
// if it does, do this
stringi = " No." + Integer.toString(i + 1);
fileName = bookName + stringi + ".txt";
book = new File(fileName);
// checks again if the book exists
try {
if (book.createNewFile()) {
out.println("Book added: " + bookName + "!");
doesHave = true;
} else {
out.println("Sorry, but we have too many copies of " + book.getName() + ". Here is your book back!");
tooMany = true;
doesHave = true;
}
} catch (IOException e) {
canMake = false;
out.println("An error occurred while donating your book.");
break;
}
}
} catch (IOException e) {
canMake = false;
out.println("An error occurred while donating your book.");
break;
}
}
if (canMake == true && tooMany == false) {
try {
FileWriter myWriter = new FileWriter(fileName);
myWriter.write(bookName + " - " + pageNum + " pages long - Genera: " + bookGenera + "\n");
myWriter.close();
System.out.println("Successfully added the book.");
} catch (IOException e) {
out.println("An error occurred while adding your book to the shelves.");
}
}