From f8cf3be87fb663a9adcb39271ee178727722de11 Mon Sep 17 00:00:00 2001 From: KHobbits Date: Mon, 14 Jan 2013 08:21:03 +0000 Subject: Better handle initial book.txt creation. --- .../earth2me/essentials/textreader/BookInput.java | 62 ++++++++++++---------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/textreader/BookInput.java b/Essentials/src/com/earth2me/essentials/textreader/BookInput.java index d8df79ec2..b0fedbf86 100644 --- a/Essentials/src/com/earth2me/essentials/textreader/BookInput.java +++ b/Essentials/src/com/earth2me/essentials/textreader/BookInput.java @@ -22,7 +22,39 @@ public class BookInput implements IText { file = new File(ess.getDataFolder(), filename + ".txt"); } - if (file.exists()) + if (!file.exists()) + { + if (createFile) + { + final InputStream input = ess.getResource(filename + ".txt"); + final OutputStream output = new FileOutputStream(file); + try + { + final byte[] buffer = new byte[1024]; + int length = input.read(buffer); + while (length > 0) + { + output.write(buffer, 0, length); + length = input.read(buffer); + } + } + finally + { + output.close(); + input.close(); + } + ess.getLogger().info("File " + filename + ".txt does not exist. Creating one for you."); + } + } + if (!file.exists()) + { + lastChange = 0; + lines = Collections.emptyList(); + chapters = Collections.emptyList(); + bookmarks = Collections.emptyMap(); + throw new FileNotFoundException("Could not create " + filename + ".txt"); + } + else { lastChange = file.lastModified(); boolean readFromfile; @@ -74,34 +106,6 @@ public class BookInput implements IText } } } - else - { - lastChange = 0; - lines = Collections.emptyList(); - chapters = Collections.emptyList(); - bookmarks = Collections.emptyMap(); - if (createFile) - { - final InputStream input = ess.getResource(filename + ".txt"); - final OutputStream output = new FileOutputStream(file); - try - { - final byte[] buffer = new byte[1024]; - int length = input.read(buffer); - while (length > 0) - { - output.write(buffer, 0, length); - length = input.read(buffer); - } - } - finally - { - output.close(); - input.close(); - } - throw new FileNotFoundException("File " + filename + ".txt does not exist. Creating one for you."); - } - } } @Override -- cgit v1.2.3