ProfilesConfigFileWriter { static void modifyProfiles(File destination, Map<String, Profile> modifications) { final boolean inPlaceModify = destination.exists(); File stashLocation = null; if (inPlaceModify) { boolean stashed = false; try { stashLocation = new File(destination.getParentFile(), destination.getName() + ".bak." + UUID.randomUUID().toString()); stashed = destination.renameTo(stashLocation); if (LOG.isDebugEnabled()) { LOG.debug(String .format("The original credentials file is stashed to location (%s).", stashLocation.getAbsolutePath())); } } finally { if (!stashed) { throw new SdkClientException( "Failed to stash the existing credentials file " + "before applying the changes."); } } } OutputStreamWriter writer = null; try { writer = new OutputStreamWriter(new FileOutputStream(destination), StringUtils.UTF8); ProfilesConfigFileWriterHelper writerHelper = new ProfilesConfigFileWriterHelper(writer, modifications); if (inPlaceModify) { Scanner existingContent = new Scanner(stashLocation, StringUtils.UTF8.name()); writerHelper.writeWithExistingContent(existingContent); } else { writerHelper.writeWithoutExistingContent(); } new ProfilesConfigFile(destination); if ( inPlaceModify && !stashLocation.delete() ) { if (LOG.isDebugEnabled()) { LOG.debug(String .format("Successfully modified the credentials file. But failed to " + "delete the stashed copy of the original file (%s).", stashLocation.getAbsolutePath())); } } } catch (Exception e) { if (inPlaceModify) { boolean restored = false; try { if ( !destination.delete() ) { LOG.debug("Unable to remove the credentials file " + "before restoring the original one."); } restored = stashLocation.renameTo(destination); } finally { if (!restored) { throw new SdkClientException( "Unable to restore the original credentials file. " + "File content stashed in " + stashLocation.getAbsolutePath()); } } } throw new SdkClientException( "Unable to modify the credentials file. " + "(The original file has been restored.)", e); } finally { try { if (writer != null) writer.close(); } catch (IOException e) {} } }  static void dumpToFile(File destination, boolean overwrite, Profile... profiles); static void modifyOrInsertProfiles(File destination, Profile... profiles); static void modifyOneProfile(File destination, String profileName, Profile newProfile); static void deleteProfiles(File destination, String... profileNames);  }
@Test public void testModifyAndInsertProfile_WithComments() throws IOException, URISyntaxException { File credWithComments = ProfileResourceLoader.profilesWithComments().asFile(); File tmpFile = copyToTempFile(credWithComments); String originalContent = FileUtils.readFileToString(tmpFile); Profile[] expected = { new Profile("a", basicCredA), new Profile("b", basicCredB), new Profile("c", sessionCredC), new Profile("d", sessionCredD) }; Profile[] modified = { new Profile("A", basicCredB), new Profile("B", basicCredA), new Profile("C", sessionCredD), new Profile("D", sessionCredC) }; ProfilesConfigFileWriter.modifyProfiles(tmpFile, ImmutableMapParameter .of("a", modified[0], "b", modified[1], "c", modified[2], "d", modified[3])); checkCredentialsFile(tmpFile, modified); String modifiedContent = FileUtils.readFileToString(tmpFile); assertFalse(originalContent.equals(modifiedContent)); ProfilesConfigFileWriter.modifyProfiles(tmpFile, ImmutableMapParameter .of("A", expected[0], "B", expected[1], "C", expected[2], "D", expected[3])); checkCredentialsFile(tmpFile, expected); String restoredContent = FileUtils.readFileToString(tmpFile); assertEquals(originalContent, restoredContent); }