FileIOUtils { public static Set<String> readStringsAsSet(InputStream stream, boolean unescape) throws IOException { BufferedReader reader = null; Set<String> set = newHashSet(); boolean threw = true; try { reader = new BufferedReader(new InputStreamReader(stream, Charsets.UTF_8)); String line = null; while ((line = reader.readLine()) != null) { if (unescape) { set.add(unescapeLineBreaks(line)); } else { set.add(line); } } threw = false; } finally { close(reader, threw); } return set; } 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 writeReadStrings() throws Exception { Set<String> added = newHashSet("a", "z", "e", "b"); File f = assertWrite(added.iterator(), false, added.size()); Set<String> retrieved = readStringsAsSet(new FileInputStream(f), false); assertEquals(added, retrieved); }
@Test public void writeReadStringsWithLineBreaks() throws IOException { Set<String> added = newHashSet(getLineBreakStrings()); File f = assertWrite(added.iterator(), true, added.size()); Set<String> retrieved = readStringsAsSet(new FileInputStream(f), true); assertEquals(added, retrieved); }
@Test public void writeReadRandomStrings() throws Exception { Set<String> added = newHashSet(); for (int i = 0; i < 100; i++) { added.add(getRandomTestString()); } File f = assertWrite(added.iterator(), true, added.size()); Set<String> retrieved = readStringsAsSet(new FileInputStream(f), true); assertEquals(added, retrieved); }