下面列出了com.google.common.io.CharSink#write ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String args[])
throws ClassNotFoundException, IOException, NoSuchMethodException {
Method latestVersionMethod = GoogleAdsAllVersions.class.getMethod("getLatestVersion");
Class cls = Class.forName(latestVersionMethod.getReturnType().getName());
List<Method> methods = Arrays.asList(cls.getDeclaredMethods());
StringBuilder output = new StringBuilder();
for (Method method : methods) {
output.append(method + "\n");
}
System.out.println("Writing the following methods to file:");
System.out.printf(output.toString());
File file = new File("./google-ads/src/test/resources/testdata/avail_service_clients.txt");
CharSink sink = Files.asCharSink(file, Charsets.UTF_8);
sink.write(output);
}
@Override
public void writeQueries(final CorpusQuerySet2016 queries,
final CharSink sink) throws IOException {
final StringBuilder sb = new StringBuilder();
int numEntryPoints = 0;
for (final CorpusQuery2016 query : QUERY_ORDERING.sortedCopy(queries)) {
for (final CorpusQueryEntryPoint entryPoint : ENTRY_POINT_ORDERING
.sortedCopy(query.entryPoints())) {
sb.append(entryPointString(query, entryPoint)).append("\n");
++numEntryPoints;
}
}
log.info("Writing {} queries with {} entry points to {}", queries.queries().size(),
numEntryPoints, sink);
sink.write(sb.toString());
}
@Test
public void givenUsingGuava_whenConvertingByteArrayIntoWriter_thenCorrect() throws IOException {
final byte[] initialArray = "With Guava".getBytes();
final String buffer = new String(initialArray);
final StringWriter stringWriter = new StringWriter();
final CharSink charSink = new CharSink() {
@Override
public final Writer openStream() throws IOException {
return stringWriter;
}
};
charSink.write(buffer);
stringWriter.close();
assertEquals("With Guava", stringWriter.toString());
}
/**
* Format the given input (a Java compilation unit) into the output stream.
*
* @throws FormatterException if the input cannot be parsed
*/
public void formatSource(CharSource input, CharSink output)
throws FormatterException, IOException {
// TODO(cushon): proper support for streaming input/output. Input may
// not be feasible (parsing) but output should be easier.
output.write(formatSource(input.read()));
}
/**
* Format the given input (a Java compilation unit) into the output stream.
*
* @throws FormatterException if the input cannot be parsed
*/
public void formatSource(CharSource input, CharSink output)
throws FormatterException, IOException {
// TODO(cushon): proper support for streaming input/output. Input may
// not be feasible (parsing) but output should be easier.
output.write(formatSource(input.read()));
}
/**
* Format the given input (a Java compilation unit) into the output stream.
*
* @throws FormatterException if the input cannot be parsed
*/
public void formatSource(CharSource input, CharSink output)
throws FormatterException, IOException {
// TODO(cushon): proper support for streaming input/output. Input may
// not be feasible (parsing) but output should be easier.
output.write(formatSource(input.read()));
}
private static void logCoverage(String leftName, Set<Symbol> leftDocIds, String rightName,
Set<Symbol> rightDocIds,
CharSink out) throws IOException {
final String msg = String.format(
"%d documents in %s; %d in %s. %d in common, %d left-only, %d right-only",
leftDocIds.size(), leftName, rightDocIds.size(), rightName,
Sets.intersection(leftDocIds, rightDocIds).size(),
Sets.difference(leftDocIds, rightDocIds).size(),
Sets.difference(rightDocIds, leftDocIds).size());
log.info(msg);
out.write(msg);
}
@Test
public void whenWriteUsingCharSink_thenWritten() throws IOException {
final String expectedValue = "Hello world";
final File file = new File("src/test/resources/test.out");
final CharSink sink = Files.asCharSink(file, Charsets.UTF_8);
sink.write(expectedValue);
final String result = Files.toString(file, Charsets.UTF_8);
assertEquals(expectedValue, result);
}
@Test
public void whenAppendToFileUsingGuava_thenCorrect() throws IOException {
File file = new File(fileName);
CharSink chs = com.google.common.io.Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND);
chs.write("Spain\r\n");
assertThat(StreamUtils.getStringFromInputStream(new FileInputStream(fileName))).isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
}
/**
* Hook called when the processd file is not compliant with the formatter.
*
* @param file the file that is not compliant
* @param formatted the corresponding formatted of the file.
*/
@Override
protected void onNonComplyingFile(File file, String formatted) throws IOException {
CharSink sink = Files.asCharSink(file, Charsets.UTF_8);
sink.write(formatted);
}