com.google.common.io.Files.asCharSink()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(11.5k)|赞(0)|评价(0)|浏览(178)

本文整理了Java中com.google.common.io.Files.asCharSink()方法的一些代码示例,展示了Files.asCharSink()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Files.asCharSink()方法的具体详情如下:
包路径:com.google.common.io.Files
类名称:Files
方法名:asCharSink

Files.asCharSink介绍

[英]Returns a new CharSink for writing character data to the given file using the given character set. The given modes control how the file is opened for writing. When no mode is provided, the file will be truncated before writing. When the FileWriteMode#APPEND mode is provided, writes will append to the end of the file without truncating it.
[中]返回用于使用给定字符集将字符数据写入给定文件的新字符接收器。给定的模式控制如何打开文件进行写入。当没有提供模式时,文件将在写入之前被截断。当提供FileWriteMode#APPEND模式时,写入操作将附加到文件末尾,而不会截断它。

代码示例

代码示例来源:origin: google/guava

/**
 * Writes a character sequence (such as a string) to a file using the given character set.
 *
 * @param from the character sequence to write
 * @param to the destination file
 * @param charset the charset used to encode the output stream; see {@link StandardCharsets} for
 *     helpful predefined constants
 * @throws IOException if an I/O error occurs
 * @deprecated Prefer {@code asCharSink(to, charset).write(from)}. This method is scheduled to be
 *     removed in January 2019.
 */
@Deprecated
public static void write(CharSequence from, File to, Charset charset) throws IOException {
 asCharSink(to, charset).write(from);
}

代码示例来源:origin: google/guava

/**
 * Appends a character sequence (such as a string) to a file using the given character set.
 *
 * @param from the character sequence to append
 * @param to the destination file
 * @param charset the charset used to encode the output stream; see {@link StandardCharsets} for
 *     helpful predefined constants
 * @throws IOException if an I/O error occurs
 * @deprecated Prefer {@code asCharSink(to, charset, FileWriteMode.APPEND).write(from)}. This
 *     method is scheduled to be removed in January 2019.
 */
@Deprecated
public
static void append(CharSequence from, File to, Charset charset) throws IOException {
 asCharSink(to, charset, FileWriteMode.APPEND).write(from);
}

代码示例来源:origin: google/j2objc

/**
 * Appends a character sequence (such as a string) to a file using the given character set.
 *
 * @param from the character sequence to append
 * @param to the destination file
 * @param charset the charset used to encode the output stream; see {@link StandardCharsets} for
 *     helpful predefined constants
 * @throws IOException if an I/O error occurs
 * @deprecated Prefer {@code asCharSink(to, charset, FileWriteMode.APPEND).write(from)}. This
 *     method is scheduled to be removed in January 2019.
 */
@Deprecated
public static void append(CharSequence from, File to, Charset charset) throws IOException {
 asCharSink(to, charset, FileWriteMode.APPEND).write(from);
}

代码示例来源:origin: google/j2objc

/**
 * Writes a character sequence (such as a string) to a file using the given character set.
 *
 * @param from the character sequence to write
 * @param to the destination file
 * @param charset the charset used to encode the output stream; see {@link StandardCharsets} for
 *     helpful predefined constants
 * @throws IOException if an I/O error occurs
 * @deprecated Prefer {@code asCharSink(to, charset).write(from)}. This method is scheduled to be
 *     removed in January 2019.
 */
@Deprecated
public static void write(CharSequence from, File to, Charset charset) throws IOException {
 asCharSink(to, charset).write(from);
}

代码示例来源:origin: robolectric/robolectric

@Implementation
protected static void stopMethodTracing() {
 if (!tracingStarted) {
  throw new RuntimeException("Tracing is not started.");
 }
 try {
  Files.asCharSink(new File(tracingFilename), Charset.forName("UTF-8")).write("trace data");
 } catch (IOException e) {
  throw new RuntimeException("Writing trace file failed", e);
 }
 tracingStarted = false;
 tracingFilename = null;
}

代码示例来源:origin: wildfly/wildfly

/**
 * Appends a character sequence (such as a string) to a file using the given character set.
 *
 * @param from the character sequence to append
 * @param to the destination file
 * @param charset the charset used to encode the output stream; see {@link StandardCharsets} for
 *     helpful predefined constants
 * @throws IOException if an I/O error occurs
 * @deprecated Prefer {@code asCharSink(to, charset, FileWriteMode.APPEND).write(from)}. This
 *     method is scheduled to be removed in January 2019.
 */
@Deprecated
public static void append(CharSequence from, File to, Charset charset) throws IOException {
 asCharSink(to, charset, FileWriteMode.APPEND).write(from);
}

代码示例来源:origin: wildfly/wildfly

/**
 * Writes a character sequence (such as a string) to a file using the given character set.
 *
 * @param from the character sequence to write
 * @param to the destination file
 * @param charset the charset used to encode the output stream; see {@link StandardCharsets} for
 *     helpful predefined constants
 * @throws IOException if an I/O error occurs
 * @deprecated Prefer {@code asCharSink(to, charset).write(from)}. This method is scheduled to be
 *     removed in January 2019.
 */
@Deprecated
public static void write(CharSequence from, File to, Charset charset) throws IOException {
 asCharSink(to, charset).write(from);
}

代码示例来源:origin: google/guava

@Override
public CharSink createSink() throws IOException {
 File file = createFile();
 if (initialString != null) {
  Writer writer = new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8);
  try {
   writer.write(initialString);
  } finally {
   writer.close();
  }
  return Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND);
 }
 return Files.asCharSink(file, Charsets.UTF_8);
}

代码示例来源:origin: CalebFenton/simplify

public static void main(String[] args) throws Exception {
  if (args.length != 2) {
    System.out.println("Usage: frameworkJarBuilder <framework resource path (e.g. android-25)> <output jar path>");
    System.exit(-1);
  }
  String resPath = FRAMEWORK_ROOT + "/" + args[0];
  System.out.println("Building framework cache from " + resPath);
  String cache = buildFrameworkCache(resPath);
  String cacheFileName = "src/main/resources/framework_classes.cfg";
  Files.asCharSink(new File(cacheFileName), Charset.forName("UTF-8")).write(cache);
  System.out.println("Saved cache to " + cacheFileName + " (" + cache.getBytes().length + " bytes)");
  String outPath = args[1];
  System.out.println("Building framework JAR");
  ClassManager classManager = new ClassManagerFactory().build();
  ClassBuilder builder = new ClassBuilder(classManager);
  Set<String> classNames = buildJar(classManager, builder, outPath);
  System.out.println("Saved " + classNames.size() + " classes to " + outPath);
}

代码示例来源:origin: prestodb/presto

private static File createAvroSchemaFile()
    throws Exception
{
  File schemaFile = File.createTempFile("avro_single_column-", ".avsc");
  String schema = "{\n" +
      "  \"namespace\": \"com.facebook.test\",\n" +
      "  \"name\": \"single_column\",\n" +
      "  \"type\": \"record\",\n" +
      "  \"fields\": [\n" +
      "    { \"name\":\"string_col\", \"type\":\"string\" }\n" +
      "]}";
  asCharSink(schemaFile, UTF_8).write(schema);
  return schemaFile;
}

代码示例来源:origin: robolectric/robolectric

private AndroidManifest newConfigWith(String fileName, String usesSdkAttrs) throws IOException {
 String contents = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
   "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
   "          package=\"org.robolectric\">\n" +
   "    <uses-sdk " + usesSdkAttrs + "/>\n" +
   "</manifest>\n";
 File f = temporaryFolder.newFile(fileName);
 Files.asCharSink(f, Charsets.UTF_8).write(contents);
 return new AndroidManifest(f.toPath(), null, null);
}

代码示例来源:origin: robolectric/robolectric

private AndroidManifest newConfigWith(String packageName, String contents) throws IOException {
 String fileContents = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
   "<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
   "          package=\"" + packageName + "\">\n" +
   "    " + contents + "\n" +
   "</manifest>\n";
 File f = temporaryFolder.newFile("whatever.xml");
 Files.asCharSink(f, Charsets.UTF_8).write(fileContents);
 return new AndroidManifest(f.toPath(), null, null);
}

代码示例来源:origin: floragunncom/search-guard

Files.asCharSink(dfile, StandardCharsets.UTF_8).write(sb);
  System.out.println("Diagnostic trace written to: "+dfile.getAbsolutePath());
} catch (Exception e1) {

代码示例来源:origin: robolectric/robolectric

@Test
 public void ioUtils() throws Exception {

  File file = temporaryFolder.newFile("test_file.txt");
  Files.asCharSink(file, StandardCharsets.UTF_8).write("some contents");

  String contents = IoUtils.readFileAsString(file.getAbsolutePath());
  assertThat(contents).isEqualTo("some contents");
 }
}

代码示例来源:origin: robolectric/robolectric

@Test public void pass_multiple_res_dirs_in_file() throws Exception {
  String resDirectoriesFileName = "res-directories";
  File resDirectoriesFile = tempFolder.newFile(resDirectoriesFileName);
  Files.asCharSink(resDirectoriesFile, Charsets.UTF_8).write("buck/res1\nbuck/res2");
  System.setProperty(
    "buck.robolectric_res_directories", "@" + resDirectoriesFile.getAbsolutePath());

  String assetDirectoriesFileName = "asset-directories";
  File assetDirectoriesFile = tempFolder.newFile(assetDirectoriesFileName);
  Files.asCharSink(assetDirectoriesFile, Charsets.UTF_8).write("buck/assets1\nbuck/assets2");
  System.setProperty(
    "buck.robolectric_assets_directories", "@" + assetDirectoriesFile.getAbsolutePath());

  ManifestIdentifier manifestIdentifier = buckManifestFactory.identify(configBuilder.build());
  AndroidManifest manifest = RobolectricTestRunner.createAndroidManifest(manifestIdentifier);
  assertThat(manifest.getResDirectory()).isEqualTo(Paths.get("buck/res2"));
  assertThat(manifest.getAssetsDirectory()).isEqualTo(Paths.get("buck/assets2"));

  List<ResourcePath> resourcePathList = manifest.getIncludedResourcePaths();
  assertThat(resourcePathList.size()).isEqualTo(3);
  assertThat(resourcePathList)
    .containsExactly(
      new ResourcePath(
        manifest.getRClass(), Paths.get("buck/res2"), Paths.get("buck/assets2")),
      new ResourcePath(manifest.getRClass(), Paths.get("buck/res1"), null),
      new ResourcePath(manifest.getRClass(), null, Paths.get("buck/assets1")));
 }
}

代码示例来源:origin: eclipse/buildship

private static void storeCacheVersionsFile(String json, File cacheFile) {
  //noinspection ResultOfMethodCallIgnored
  cacheFile.getParentFile().mkdirs();
  try {
    CharSource.wrap(json).copyTo(Files.asCharSink(cacheFile, Charsets.UTF_8));
  } catch (IOException e) {
    LOG.error("Cannot write Gradle version information cache file.", e);
    // do not throw an exception if cache file cannot be written to be more robust against file system problems
  }
}

代码示例来源:origin: zhengjunbase/codehelper.generator

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);
}

代码示例来源:origin: com.github.mike10004/har-replay-test-support

public void writeResponseFilesInDirectory(Path directory) throws IOException {
  File urlFile = directory.resolve("url.txt").toFile();
  Files.asCharSink(urlFile, StandardCharsets.UTF_8).write(url);
  String dataFilename = constructResponseDataFilename();
  File dataFile = directory.resolve(dataFilename).toFile();
  responseContent.copyTo(Files.asByteSink(dataFile));
}

代码示例来源:origin: line/centraldogma

private static void addToGitIndex(Git git, File gitWorkTree,
                   String path, String content) throws IOException, GitAPIException {
    final File file = Paths.get(gitWorkTree.getAbsolutePath(), path.split("/")).toFile();
    file.getParentFile().mkdirs();
    Files.asCharSink(file, StandardCharsets.UTF_8).write(content);
    git.add().addFilepattern(path).call();
  }
}

代码示例来源:origin: googleads/googleads-java-lib

@Before
public void setUp() throws Exception {
 csvStringFile = tempFolder.newFile();
 Files.asCharSink(csvStringFile, StandardCharsets.UTF_8).write(csvString);
 if (headerPresent) {
  dataList.remove(0);
 }
}

相关文章