下面列出了com.google.common.io.Files#asCharSink ( ) 实例代码,或者点击链接到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);
}
BatchFileAppender(final File file, final int capacity) {
this.sink = Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND);
this.pendingWrite = new ArrayBlockingQueue<>(capacity);
this.batchWriteThread = new Thread(new Runnable() {
@Override
public void run() {
batchWriteLoop();
}
});
this.stop = false;
this.batchWriteThread.start();
}
private static void writeToFile(String filePath, List<String> lines) {
CharSink outputSink = Files.asCharSink(new File(filePath), StandardCharsets.UTF_8);
try {
outputSink.writeLines(lines);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void process(Context context, String templateName, String targetPath) throws IOException {
URL templateUrl = Resources.getResource(IpmiCommandNameTest.class, templateName);
CharSource source = Resources.asCharSource(templateUrl, StandardCharsets.UTF_8);
File file = new File(targetPath);
CharSink sink = Files.asCharSink(file, StandardCharsets.UTF_8);
try (Reader r = source.openBufferedStream()) {
try (Writer w = sink.openBufferedStream()) {
engine.evaluate(context, w, file.getName(), r);
}
}
}
@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 whenWriteMultipleLinesUsingCharSink_thenWritten() throws IOException {
final List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
final File file = new File("src/test/resources/test.out");
final CharSink sink = Files.asCharSink(file, Charsets.UTF_8);
sink.writeLines(names, " ");
final String result = Files.toString(file, Charsets.UTF_8);
final String expectedValue = Joiner.on(" ").join(names);
assertEquals(expectedValue, result.trim());
}
public static void writeLines(String fileName,List<String> list,Charset charset) throws IOException {
CharSink cs = Files.asCharSink(new File(fileName), charset);
list = PojoUtil.avoidEmptyList(list);
cs.writeLines(list);
}
public static void writeLines(File file,List<String> list,Charset charset) throws IOException {
CharSink cs = Files.asCharSink(file, charset);
list = PojoUtil.avoidEmptyList(list);
cs.writeLines(list);
}
/**
* 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);
}