ProfilesConfigFileWriter { public static void dumpToFile(File destination, boolean overwrite, Profile... profiles) { if (destination.exists() && !overwrite) { throw new SdkClientException( "The destination file already exists. " + "Set overwrite=true if you want to clobber the existing " + "content and completely re-write the file."); } OutputStreamWriter writer; try { writer = new OutputStreamWriter(new FileOutputStream(destination, false), StringUtils.UTF8); } catch (IOException ioe) { throw new SdkClientException( "Unable to open the destination file.", ioe); } try { final Map<String, Profile> modifications = new LinkedHashMap<String, Profile>(); for (Profile profile : profiles) { modifications.put(profile.getProfileName(), profile); } ProfilesConfigFileWriterHelper writerHelper = new ProfilesConfigFileWriterHelper(writer, modifications); writerHelper.writeWithoutExistingContent(); } finally { try { writer.close(); } catch (IOException ioe) {} } }  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 testDumpToFile() throws IOException { File tmpFile = File.createTempFile("credentials.", null); Profile[] abcd = { new Profile("a", basicCredA), new Profile("b", basicCredB), new Profile("c", sessionCredC), new Profile("d", sessionCredD) }; ProfilesConfigFileWriter.dumpToFile(tmpFile, true, abcd); checkCredentialsFile(tmpFile, abcd); Profile[] a = { new Profile("a", basicCredA) }; ProfilesConfigFileWriter.dumpToFile(tmpFile, true, a); checkCredentialsFile(tmpFile, a); try { ProfilesConfigFileWriter.dumpToFile(tmpFile, false, new Profile("a", basicCredA)); fail("Should have thrown exception since the destination file already exists."); } catch (AmazonClientException expected) {} }