public class AAA { private String a; private String b; public String getA() { return a; } public void setA(String a) { this.a = a; } public String getB() { return b; } public void setB(String b) { this.b = b; } }
public AAA read(final String path) { File file = new File(path); if (file.exists()) { try { XMLDecoder decoder = new XMLDecoder(new FileInputStream(file)); AAA a = (AAA) decoder.readObject(); return a; } catch (FileNotFoundException e) { log.error("加载配置信息异常:" + file.getPath(), e); throw new RuntimeException("加载配置信息失败:" + e.getMessage()); } } else { throw new RuntimeException("找不到系统配置文件:" + file.getPath()); } } public void save(AAA a, final String path) { File file = new File(path); try { XMLEncoder encoder = new XMLEncoder(new FileOutputStream(file)); encoder.writeObject(a); encoder.close(); } catch (Exception e) { log.error("保存配置文件异常:" + file.getPath()); throw new RuntimeException("保存配置文件失败:" + e.getMessage()); } }
本博客文章绝大多数为原创,少量为转载,代码经过测试验证,如果有疑问直接留言或者私信我。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。
创作文章不容易,转载文章必须注明文章出处;如果这篇文章对您有帮助,点击右侧打赏,支持一下吧。
浙公网安备 33010602011771号