/** * @param template * @param fileName * @return * @return */ public static <T extends FileManagerWriteable> T readFile(T template, String fileName) { if (template == null) throw new NullPointerException( "Template must be a valid, instantiated object of the desired type."); File file = new File( template.getFileType().getPath() + fileName + template.getFileType().getExtension()); if (file.exists() && !file.isDirectory()) { try { BufferedReader reader = new BufferedReader(new FileReader(file)); ArrayList<String> lines = new ArrayList<String>(); String line = reader.readLine(); while (line != null) { lines.add(line); line = reader.readLine(); } reader.close(); byte[][] bytes = new byte[lines.size()][]; for (int i = 0, stringIndex = 0; i < bytes.length; i++, stringIndex++) { if (lines.get(stringIndex) == null || lines.get(stringIndex) == "") { bytes = Arrays.copyOfRange(bytes, 0, bytes.length - 1); // Trimming off excess index. i--; continue; } bytes[i] = BinaryOperations.characterArrayToByteArray(lines.get(stringIndex).toCharArray()); } // Unless you purposely try and throw it a curve ball, nothing should happen. // Throwing a curve ball will be difficult, I might add, considering it should // always return an object of the same type as the template, but // since the method can be overridden, who knows. Object o = template.fromBytes(bytes); if (o != null) { @SuppressWarnings("unchecked") T result = (T) o; // Only reason I split the return statement up was so I could throw in the // unchecked annotation. return result; } } catch (IOException e) { e.printStackTrace(); } } return null; }
/** @param object */ public static void writeFile(FileManagerWriteable object) { try { checkFilePath(object.getFileType().getPath()); BufferedWriter writer = new BufferedWriter( new FileWriter( object.getFileType().getPath() + object.getFileName() + object.getFileType().getExtension())); for (byte[] a : object.toBytes()) { writer.write(BinaryOperations.byteArrayToCharacterArray(a)); writer.newLine(); } writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }