Better handle initial book.txt creation.

This commit is contained in:
KHobbits 2013-01-14 08:21:03 +00:00
parent ada266a0f9
commit f8cf3be87f

View File

@ -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