I/O
// Read the lines of a UTF-8 text file
ImmutableList<String> lines = Files.asCharSource(file, Charsets.UTF_8)
.readLines();
// Count distinct word occurrences in a file
Multiset<String> wordOccurrences = HashMultiset.create(
Splitter.on(CharMatcher.whitespace())
.trimResults()
.omitEmptyStrings()
.split(Files.asCharSource(file, Charsets.UTF_8).read()));
// SHA-1 a file
HashCode hash = Files.asByteSource(file).hash(Hashing.sha1());
// Copy the data from a URL to a file
Resources.asByteSource(url).copyTo(Files.asByteSink(file));
1.ByteStreams and CharStreams
ByteStreams |
CharStreams |
|---|---|
byte[] toByteArray(InputStream) |
String toString(Readable) |
N/A |
List<String> readLines(Readable) |
long copy(InputStream, OutputStream) |
long copy(Readable, Appendable) |
void readFully(InputStream, byte[]) |
N/A |
void skipFully(InputStream, long) |
void skipFully(Reader, long) |
OutputStream nullOutputStream() |
Writer nullWriter() |
2.Sources and sinks
Bytes |
Chars |
|---|---|
Files.asByteSource(File) |
Files.asCharSource(File, Charset) |
Files.asByteSink(File, FileWriteMode...) |
Files.asCharSink(File, Charset, FileWriteMode...) |
MoreFiles.asByteSource(Path, OpenOption...) |
MoreFiles.asCharSource(Path, Charset, OpenOption...) |
MoreFiles.asByteSink(Path, OpenOption...) |
MoreFiles.asCharSink(Path, Charset, OpenOption...) |
Resources.asByteSource(URL) |
Resources.asCharSource(URL, Charset) |
ByteSource.wrap(byte[]) |
CharSource.wrap(CharSequence) |
ByteSource.concat(ByteSource...) |
CharSource.concat(CharSource...) |
ByteSource.slice(long, long) |
N/A |
CharSource.asByteSource(Charset) |
ByteSource.asCharSource(Charset) |
N/A |
ByteSink.asCharSink(Charset) |
ByteSource |
CharSource |
|
|---|---|---|
byte[] read() |
String read() |
|
N/A |
ImmutableList<String> |
readLines() |
N/A |
String readFirstLine() |
|
long copyTo(ByteSink) |
long copyTo(CharSink) |
|
long copyTo(OutputStream) |
long copyTo(Appendable) |
|
Optional<Long> sizeIfKnown() |
Optional<Long> lengthIfKnown() |
|
long size() |
long length() |
|
boolean isEmpty() |
boolean isEmpty() |
|
boolean contentEquals(ByteSource) |
N/A |
|
HashCode hash(HashFunction) |
N/A |
3.Files
| Method | Description |
|---|---|
createParentDirs(File) |
Creates necessary but nonexistent parent directories of the file. |
getFileExtension(String) |
Gets the file extension of the file described by the path. |
getNameWithoutExtension(String) |
Gets the name of the file with its extension removed |
simplifyPath(String) |
Cleans up the path. Not always consistent with your filesystem; test carefully! |
fileTraverser() |
Returns a Traverser that can traverse file trees |
4.Closing Resources
4.1try-with-resources
Using JDK7+
try (InputStream in = openInputStream();
OutputStream out = openOutputStream()) {
// do stuff with in and out
}
4.2Sources and Sinks
ByteSink sink = ...
Files.asByteSource(file).copyTo(sink);
4.3Closer
Closer closer = Closer.create();
try {
InputStream in = closer.register(openInputStream());
OutputStream out = closer.register(openOutputStream());
// do stuff with in and out
} catch (Throwable e) { // must catch Throwable
throw closer.rethrow(e);
} finally {
closer.close();
}