public static Object readData(String filePath) { Object result = null; try { filePath = StringUtil.checkFullPathLength(filePath); FileLockManager.startFileRead(filePath); if (isValid(filePath)) { FileInputStream os = new FileInputStream(filePath); XMLDecoder decoder = new XMLDecoder(os); decoder.setExceptionListener( new ExceptionListener() { public void exceptionThrown(Exception exception) { log.error("readData(): error", exception); } }); result = decoder.readObject(); decoder.close(); } } catch (java.io.FileNotFoundException fnfe) { log.trace("readData(): file not found exception, filePath=" + filePath); } catch (Error e) { log.error("readData(): error, filePath=" + filePath, e); } catch (Exception e) { log.error("readData(): exception, filePath=" + filePath, e); } finally { FileLockManager.endFileRead(filePath); } return result; }
public static boolean saveData(Object data, String filePath) { boolean result = false; try { if (log.isTraceEnabled()) log.trace("saveData(): data=" + data + ", filePath=" + filePath); filePath = StringUtil.checkFullPathLength(filePath); FileLockManager.startFileWrite(filePath); if (filePath != null) { File file = new File(filePath); if (!file.isDirectory()) { FileOutputStream os = new FileOutputStream(filePath); XMLEncoder encoder = new XMLEncoder(os); encoder.setExceptionListener( new ExceptionListener() { public void exceptionThrown(Exception exception) { log.error("readData(): error", exception); } }); encoder.writeObject(data); encoder.close(); result = true; } else { log.warn( "saveData(): attempted to save over directory, filePath=" + filePath + ", data=" + data); } } } catch (Error e) { log.error("saveData(): error", e); } catch (Exception e) { log.error("saveData(): xception", e); } finally { FileLockManager.endFileWrite(filePath); } if (log.isTraceEnabled()) log.trace("saveData(): done saving..."); return result; }