Java Code Examples for com.google.common.io.Files#asCharSink()

The following examples show how to use com.google.common.io.Files#asCharSink() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
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);
}
 
Example 2
Source Project: qmq   File: BatchFileAppender.java    License: Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 3
Source Project: j2cl   File: BazelJ2clRta.java    License: Apache License 2.0 5 votes vote down vote up
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);
  }
}
 
Example 4
Source Project: ipmi4j   File: IpmiCommandNameTest.java    License: Apache License 2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example 5
Source Project: tutorials   File: GuavaIOUnitTest.java    License: MIT License 5 votes vote down vote up
@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);
}
 
Example 6
Source Project: tutorials   File: GuavaIOUnitTest.java    License: MIT License 5 votes vote down vote up
@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());

}
 
Example 7
Source Project: codehelper.generator   File: IOUtils.java    License: Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 8
Source Project: codehelper.generator   File: IOUtils.java    License: Apache License 2.0 4 votes vote down vote up
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);
}
 
Example 9
Source Project: fmt-maven-plugin   File: FMT.java    License: MIT License 2 votes vote down vote up
/**
 * 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);
}