Java tutorial
/** * You should have received a copy of the GNU General Public License version 3 * along with this work; Please find the Copyright information and Terms and * Conditions in the ClientLauncher.java or ServerLauncher.java file. */ package ca.live.hk12.crescent.server.service; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.nio.file.Files; import java.util.Scanner; import javax.swing.JOptionPane; import ca.live.hk12.crescent.ServerLauncher; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class FileService { // // Reading, Writing and Managing Files // @Deprecated private static int getNumLines(String fileLocation) { Scanner sc = null; int lines = -1; try { sc = new Scanner(new FileReader(fileLocation)); while (sc.hasNext()) { sc.nextLine(); lines++; } } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "File not found : " + fileLocation); } finally { sc.close(); } return ++lines; //returns -1 if error occurred. } @Deprecated public static String[] readData(String fileLocation) { Scanner sc = null; String[] data = new String[getNumLines(fileLocation)]; try { sc = new Scanner(new FileReader(fileLocation)); for (int i = 0; i < data.length; i++) { data[i] = sc.nextLine(); } } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, "File not found : " + fileLocation); } finally { sc.close(); } return data; } public static <T> T readData(String fileLocation, Class<T> dataClass) { T t = null; try (Reader reader = new InputStreamReader(new FileInputStream(fileLocation), "UTF-8")) { Gson gson = new GsonBuilder().create(); t = gson.fromJson(reader, dataClass); reader.close(); } catch (Exception e) { if (ServerLauncher.DEBUG) throw new ServerException("Unable to read file:<br>" + fileLocation); } return t; } public static <T> boolean writeData(String fileLocation, T t) { try (Writer writer = new OutputStreamWriter(new FileOutputStream(fileLocation), "UTF-8")) { Gson gson = new GsonBuilder().create(); gson.toJson(t, writer); return true; } catch (Exception e) { throw new ServerException("Could not write to:<br/>" + fileLocation); } } @Deprecated public static void writeData(String fileLocation, String data) { Writer writer = null; try { writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileLocation), "utf-8")); writer.write(data); } catch (IOException ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(null, "`Files` failed to save:\n" + data); } finally { try { writer.close(); } catch (Exception ex) { } } } public boolean renameFile(File oldFile, File newFile) { try { Files.copy(oldFile.toPath(), newFile.toPath()); } catch (IOException e) { new ServerException("Failed to renameFile: " + oldFile.getName()); return false; } return true; } // // Directory methods that should be an enum class... but too lazy T__T // /* World */ public static String getWorldDirectory() { return "data/Worlds/"; } public static String getWorldDataLocation(byte worldID) { return getWorldDirectory() + worldID + ".dat"; } public static String getWorldInfoLocation(byte worldID) { return getWorldDirectory() + worldID + ".info"; } /* Users */ public static String getUserDBDirectory() { return "data/Users/"; } public static String getUserFileLocation(String username) { return getUserDBDirectory() + username + ".dat"; } // // Making Directories. USELSESSSELSELSELELSLELIJOSEUROILSEJR:LKSEJR I'm tired qq // public static void makeDirectory(String directory) { File file = new File(directory); try { file.mkdirs(); } catch (Exception e) { throw new ServerException("Unable to make directory: " + directory); } } public static String getCurrentPlayerIDFileLocation() { return getUserDBDirectory() + "playerID.count"; } }