public void saveToFile(final File file) throws UnsupportedEncodingException, FileNotFoundException, IOException { // Marshal to a string first, then write the string to the file. This // way the original config isn't lost if the XML from the marshal is hosed. getWriteLock().lock(); final String marshalledConfig = getSnmpConfigAsString(); FileOutputStream out = null; Writer fileWriter = null; try { if (marshalledConfig != null) { out = new FileOutputStream(file); fileWriter = new OutputStreamWriter(out, StandardCharsets.UTF_8); fileWriter.write(marshalledConfig); fileWriter.flush(); fileWriter.close(); if (m_container != null) { m_container.reload(); } } } finally { IOUtils.closeQuietly(fileWriter); IOUtils.closeQuietly(out); getWriteLock().unlock(); } }