FileIOUtils { public static void sort(File file) throws IOException { File sorted = createTempFile("fleioutilssort", null); merge(sortInBatch(file, lexComparator, true), sorted); move(sorted, file); } 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 sortTest() throws IOException { List<String> list = newArrayList("a", "z", "e", "b"); File f = assertWrite(list.iterator(), false, list.size()); sort(f); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), UTF_8)); String line; List<String> retrieved = newArrayList(); while ((line = reader.readLine()) != null) { retrieved.add(line); } closeQuietly(reader); Collections.sort(list); assertArrayEquals(Arrays.toString(list.toArray()), list.toArray(), retrieved.toArray()); }
@Test public void sortLargeFileWithCustomComparatorTest() throws IOException { final int numEntries = 100000; long[] entries = new long[numEntries]; Random r = new Random(0); for (int i = 0; i < numEntries; i++) { entries[i] = r.nextLong(); } Iterator<Long> boxedEntries = Longs.asList(entries).iterator(); Iterator<String> hexEntries = Iterators.transform(boxedEntries, new Function<Long, String>() { @Nullable @Override public String apply(@Nullable Long input) { return Long.toHexString(input); } }); File f = assertWrite(hexEntries, false, numEntries); Comparator<String> prefixComparator = new Comparator<String>() { @Override public int compare(String s1, String s2) { return s1.substring(0, 3).compareTo(s2.substring(0, 3)); } }; sort(f, prefixComparator); BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), UTF_8)); String previous = reader.readLine().substring(0, 3); while (true) { String current = reader.readLine(); if (current == null) { break; } current = current.substring(0, 3); assertFalse("Distinct sort didn't filter out duplicates properly.", previous.equals(current)); assertTrue("Sort didn't create increasing order", previous.compareTo(current) < 0); previous = current; } closeQuietly(reader); }