Improve file handling.

This commit is contained in:
GJ 2012-07-03 21:21:32 -04:00
parent 51ca29ab9f
commit 4aecb9756e

View File

@ -3,6 +3,7 @@ package com.gmail.nossr50.commands.mc;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@ -118,9 +119,12 @@ public class McremoveCommand implements CommandExecutor {
private boolean removeFlatFileUser(String playerName) { private boolean removeFlatFileUser(String playerName) {
boolean worked = false; boolean worked = false;
BufferedReader in = null;
FileWriter out = null;
try { try {
FileReader file = new FileReader(location); FileReader file = new FileReader(location);
BufferedReader in = new BufferedReader(file); in = new BufferedReader(file);
StringBuilder writer = new StringBuilder(); StringBuilder writer = new StringBuilder();
String line = ""; String line = "";
@ -137,16 +141,32 @@ public class McremoveCommand implements CommandExecutor {
} }
} }
in.close(); out = new FileWriter(location); //Write out the new file
FileWriter out = new FileWriter(location); //Write out the new file
out.write(writer.toString()); out.write(writer.toString());
out.close();
return worked;
} }
catch (Exception e) { catch (Exception e) {
plugin.getLogger().severe("Exception while reading " + location + " (Are you sure you formatted it correctly?)" + e.toString()); plugin.getLogger().severe("Exception while reading " + location + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
if (in != null) {
try {
in.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
if (out != null) {
try {
out.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
return worked; return worked;
} }
}
} }