Maven Dependency
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.fool.guava</groupId> <artifactId>guava</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>guava</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
Copy
@Test
public void testCopy() throws IOException {
File original = new File("src/main/resources/original.txt");
File copy = new File("src/main/resources/copy.txt");
Files.copy(original, copy);
}
Move
@Test
public void testMove() throws IOException {
File copy = new File("src/main/resources/copy.txt");
File newFile = new File("src/main/resources/newFile.txt");
Files.move(copy, newFile);
}
Hash
@Test
public void testHashFile() throws IOException {
File file = new File("src/main/resources/hash.txt");
HashCode hashCode = Files.hash(file, Hashing.md5());
System.out.println(hashCode);
}
Write
@Test
public void testWrite() throws IOException {
File file = new File("src/main/resources/lines.txt");
Files.write("LiuBei\nGuanYu\nZhangFei", file, Charsets.UTF_8);
String result = Files.toString(file, Charsets.UTF_8);
String expectedValue = "LiuBei\nGuanYu\nZhangFei";
assertThat(result, equalTo(expectedValue));
}
ReadLines
@Test
public void testReadLines() throws IOException {
File file = new File("src/main/resources/lines.txt");
List<String> expectedLines = Lists.newArrayList("LiuBei", "GuanYu", "ZhangFei");
List<String> readLines = Files.readLines(file, Charsets.UTF_8);
assertThat(readLines, equalTo(expectedLines));
}
ReadLinesWithProcessor
@Test
public void testReadLinesWithProcessor() throws IOException {
File file = new File("src/main/resources/books.txt");
List<String> expectedLines = Lists.newArrayList("SanGuoYanYi", "ShuiHuZhuan", "XiYouJi", "HongLouMeng");
List<String> readLines = Files.readLines(file, Charsets.UTF_8, new LineProcessor<List<String>>() {
private List<String> bookTitles = Lists.newArrayList();
@Override
public boolean processLine(String line) throws IOException {
bookTitles.add(Iterables.get(Splitter.on(",").split(line), 1));
return true;
}
@Override
public List<String> getResult() {
return bookTitles;
}
});
assertThat(readLines, equalTo(expectedLines));
}
Append
@Test
public void testAppendingWritingToFile() throws IOException {
File file = new File("src/main/resources/quote.txt");
file.deleteOnExit();
String hamletQuoteStart = "To be, or not to be";
Files.write(hamletQuoteStart, file, Charsets.UTF_8);
assertThat(Files.toString(file, Charsets.UTF_8), equalTo(hamletQuoteStart));
String hamletQuoteEnd = ", that is the question";
Files.append(hamletQuoteEnd, file, Charsets.UTF_8);
assertThat(Files.toString(file, Charsets.UTF_8), equalTo(hamletQuoteStart + hamletQuoteEnd));
String overwrite = "Overwriting the file";
Files.write(overwrite, file, Charsets.UTF_8);
assertThat(Files.toString(file, Charsets.UTF_8), equalTo(overwrite));
}
ByteSink
@Test
public void testByteSink() throws IOException {
String expectedValue = "Hello World";
File file = new File("src/main/resources/helloworld.txt");
ByteSink byteSink = Files.asByteSink(file);
byteSink.write("Hello World".getBytes());
String result = Files.toString(file, Charsets.UTF_8);
assertThat(result, equalTo(expectedValue));
}
ByteSource
@Test
public void testByteSource() throws IOException {
File file = new File("src/main/resources/helloworld.txt");
ByteSource byteSource = Files.asByteSource(file);
byte[] result = byteSource.read();
assertThat(result, equalTo(Files.toByteArray(file)));
System.out.println(new String(result));
String expectedResult = "lo World";
long offset = 3;
long len = 1000;
ByteSource sliceSource = Files.asByteSource(file).slice(offset, len);
result = sliceSource.read();
assertEquals(expectedResult, new String(result));
System.out.println(new String(result));
}
CharSink
@Test
public void testCharSink() throws IOException {
String expectedValue = "Hello World";
File file = new File("src/main/resources/helloworld.txt");
CharSink charSink = Files.asCharSink(file, Charsets.UTF_8);
charSink.write("Hello World");
String result = Files.toString(file, Charsets.UTF_8);
System.out.println(result);
assertThat(result, equalTo(expectedValue));
}
CharSource
@Test
public void testCharSource() throws IOException {
String expectedValue = "Hello World";
File file = new File("src/main/resources/helloworld.txt");
CharSource charSource = Files.asCharSource(file, Charsets.UTF_8);
String result = charSource.read();
System.out.println(result);
assertThat(result, equalTo(expectedValue));
}
ByteStreams
@Test
public void testByteStreams() throws IOException {
Closer closer = Closer.create();
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(new File("src/main/resources/helloworld.txt")));
closer.register(in);
byte[] result = ByteStreams.toByteArray(in);
closer.close();
String expectedValue = "Hello World";
assertThat(new String(result), equalTo(expectedValue));
System.out.println(new String(result));
}
CharStreams
@Test
public void testCharStreams() throws IOException {
Closer closer = Closer.create();
BufferedReader reader = new BufferedReader(new FileReader(new File("src/main/resources/helloworld.txt")));
closer.register(reader);
String result = CharStreams.toString(reader);
closer.close();
String expectedValue = "Hello World";
assertThat(new String(result), equalTo(expectedValue));
System.out.println(result);
}
Closer
package org.fool.guava.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import com.google.common.io.Closer;
public class CloserTest {
public static void main(String[] args) throws IOException {
Closer closer = Closer.create();
try {
BufferedReader reader = new BufferedReader(new FileReader(new File("src/main/resources/reader.txt")));
BufferedWriter writer = new BufferedWriter(new FileWriter(new File("src/main/resources/writer.txt")));
closer.register(reader);
closer.register(writer);
String line;
while ((line = reader.readLine()) != null) {
writer.write(line);
}
} catch (Throwable t) {
throw closer.rethrow(t);
} finally {
closer.close();
}
}
}