FileIOUtils { public static void append(List<File> files, File appendTo, boolean delete) throws IOException { OutputStream appendStream = null; boolean threw = true; try { appendStream = asByteSink(appendTo, APPEND).openBufferedStream(); for (File f : files) { InputStream iStream = new FileInputStream(f); try { copyLarge(iStream, appendStream); } finally { closeQuietly(iStream); } } threw = false; } finally { if (delete) { for (File f : files) { f.delete(); } } close(appendStream, threw); } } private  FileIOUtils(); static void sort(File file); static void sort(File file, Comparator<String> comparator); static void merge(List<File> files, File output); static void merge(List<File> files, File output, Comparator<String> comparator); static File copy(InputStream stream); static void append(List<File> files, File appendTo, boolean delete); static void writeAsLine(BufferedWriter writer, String str, boolean escape); static int writeStrings(Iterator<String> iterator, File f, boolean escape); static int writeStrings(Iterator<String> iterator, File f, boolean escape,
        @Nullable Logger logger, @Nullable String message); static int writeStrings(Iterator<String> iterator, File f, boolean escape,
        @NotNull java.util.function.Function<String, String> transformer, @Nullable Logger logger, @Nullable String message); @Deprecated static int writeStrings(Iterator<String> iterator, File f, boolean escape,
            @NotNull Function<String, String> transformer, @Nullable Logger logger, @Nullable String message); static Set<String> readStringsAsSet(InputStream stream, boolean unescape); static Comparator<String> lineBreakAwareComparator(Comparator<String> delegate); static void copyInputStreamToFile(final InputStream source, final File destination); final static Comparator<String> lexComparator; @Deprecated final static Function<String, String> passThruTransformer; }
@Test public void appendTest() throws IOException { Set<String> added1 = newHashSet("a", "z", "e", "b"); File f1 = assertWrite(added1.iterator(), false, added1.size()); Set<String> added2 = newHashSet("2", "3", "5", "6"); File f2 = assertWrite(added2.iterator(), false, added2.size()); Set<String> added3 = newHashSet("t", "y", "8", "9"); File f3 = assertWrite(added3.iterator(), false, added3.size()); append(newArrayList(f2, f3), f1, true); assertEquals(union(union(added1, added2), added3), readStringsAsSet(new FileInputStream(f1), false)); assertTrue(!f2.exists()); assertTrue(!f3.exists()); assertTrue(f1.exists()); }
@Test public void appendTestFileDeleteOnError() throws IOException { Set<String> added2 = newHashSet("2", "3", "5", "6"); File f2 = assertWrite(added2.iterator(), false, added2.size()); Set<String> added3 = newHashSet("t", "y", "8", "9"); File f3 = assertWrite(added3.iterator(), false, added3.size()); try { append(newArrayList(f2, f3), null, true); } catch (Exception e) { } assertTrue(!f2.exists()); assertTrue(!f3.exists()); }