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

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

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

Files.asCharSource介绍

[英]Returns a new CharSource for reading character data from the given file using the given character set.
[中]返回用于使用给定字符集从给定文件读取字符数据的新字符源。

代码示例

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

/**
 * Reads all characters from a file into a {@link String}, using the given character set.
 *
 * @param file the file to read from
 * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
 *     helpful predefined constants
 * @return a string containing all the characters from the file
 * @throws IOException if an I/O error occurs
 * @deprecated Prefer {@code asCharSource(file, charset).read()}. This method is scheduled to be
 *     removed in January 2019.
 */
@Deprecated
public static String toString(File file, Charset charset) throws IOException {
 return asCharSource(file, charset).read();
}

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

/**
 * Copies all characters from a file to an appendable object, using the given character set.
 *
 * @param from the source file
 * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
 *     helpful predefined constants
 * @param to the appendable object
 * @throws IOException if an I/O error occurs
 * @deprecated Prefer {@code asCharSource(from, charset).copyTo(to)}. This method is scheduled to
 *     be removed in January 2019.
 */
@Deprecated
public
static void copy(File from, Charset charset, Appendable to) throws IOException {
 asCharSource(from, charset).copyTo(to);
}

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

/**
 * Reads the first line from a file. The line does not include line-termination characters, but
 * does include other leading and trailing whitespace.
 *
 * @param file the file to read from
 * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
 *     helpful predefined constants
 * @return the first line, or null if the file is empty
 * @throws IOException if an I/O error occurs
 * @deprecated Prefer {@code asCharSource(file, charset).readFirstLine()}. This method is
 *     scheduled to be removed in January 2019.
 */
@Deprecated
public
static String readFirstLine(File file, Charset charset) throws IOException {
 return asCharSource(file, charset).readFirstLine();
}

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

return asCharSource(file, charset)
  .readLines(
    new LineProcessor<List<String>>() {

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

/**
 * Copies all characters from a file to an appendable object, using the given character set.
 *
 * @param from the source file
 * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
 *     helpful predefined constants
 * @param to the appendable object
 * @throws IOException if an I/O error occurs
 * @deprecated Prefer {@code asCharSource(from, charset).copyTo(to)}. This method is scheduled to
 *     be removed in January 2019.
 */
@Deprecated
public static void copy(File from, Charset charset, Appendable to) throws IOException {
 asCharSource(from, charset).copyTo(to);
}

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

/**
 * Reads all characters from a file into a {@link String}, using the given character set.
 *
 * @param file the file to read from
 * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
 *     helpful predefined constants
 * @return a string containing all the characters from the file
 * @throws IOException if an I/O error occurs
 * @deprecated Prefer {@code asCharSource(file, charset).read()}. This method is scheduled to be
 *     removed in January 2019.
 */
@Deprecated
public static String toString(File file, Charset charset) throws IOException {
 return asCharSource(file, charset).read();
}

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

/**
 * Reads the first line from a file. The line does not include line-termination characters, but
 * does include other leading and trailing whitespace.
 *
 * @param file the file to read from
 * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
 *     helpful predefined constants
 * @return the first line, or null if the file is empty
 * @throws IOException if an I/O error occurs
 * @deprecated Prefer {@code asCharSource(file, charset).readFirstLine()}. This method is
 *     scheduled to be removed in January 2019.
 */
@Deprecated
public static String readFirstLine(File file, Charset charset) throws IOException {
 return asCharSource(file, charset).readFirstLine();
}

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

/**
 * Streams lines from a {@link File}, stopping when our callback returns false, or we have read
 * all of the lines.
 *
 * @param file the file to read from
 * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
 *     helpful predefined constants
 * @param callback the {@link LineProcessor} to use to handle the lines
 * @return the output of processing the lines
 * @throws IOException if an I/O error occurs
 * @deprecated Prefer {@code asCharSource(file, charset).readLines(callback)}. This method is
 *     scheduled to be removed in January 2019.
 */
@Deprecated
@CanIgnoreReturnValue // some processors won't return a useful result
public
static <T> T readLines(File file, Charset charset, LineProcessor<T> callback) throws IOException {
 return asCharSource(file, charset).readLines(callback);
}

代码示例来源:origin: googleapis/google-cloud-java

private static String getActiveGoogleCloudConfig(File configDir) {
 String activeGoogleCloudConfig = null;
 try {
  activeGoogleCloudConfig =
    Files.asCharSource(new File(configDir, "active_config"), Charset.defaultCharset())
      .readFirstLine();
 } catch (IOException ex) {
  // ignore
 }
 // if reading active_config failed or the file is empty we try default
 return firstNonNull(activeGoogleCloudConfig, "default");
}

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

/**
 * Copies all characters from a file to an appendable object, using the given character set.
 *
 * @param from the source file
 * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
 *     helpful predefined constants
 * @param to the appendable object
 * @throws IOException if an I/O error occurs
 * @deprecated Prefer {@code asCharSource(from, charset).copyTo(to)}. This method is scheduled to
 *     be removed in January 2019.
 */
@Deprecated
public static void copy(File from, Charset charset, Appendable to) throws IOException {
 asCharSource(from, charset).copyTo(to);
}

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

/**
 * Reads the first line from a file. The line does not include line-termination characters, but
 * does include other leading and trailing whitespace.
 *
 * @param file the file to read from
 * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
 *     helpful predefined constants
 * @return the first line, or null if the file is empty
 * @throws IOException if an I/O error occurs
 * @deprecated Prefer {@code asCharSource(file, charset).readFirstLine()}. This method is
 *     scheduled to be removed in January 2019.
 */
@Deprecated
public static String readFirstLine(File file, Charset charset) throws IOException {
 return asCharSource(file, charset).readFirstLine();
}

代码示例来源:origin: apache/incubator-druid

private ImmutableSortedMap<String, ImmutableList<String>> readMap(final String mapPath)
{
 String fileContent;
 String actualPath = mapPath;
 try {
  if (Strings.isNullOrEmpty(mapPath)) {
   actualPath = this.getClass().getClassLoader().getResource("defaultWhiteListMap.json").getFile();
   LOGGER.info("using default whiteList map located at [%s]", actualPath);
   InputStream byteContent = this.getClass().getClassLoader().getResourceAsStream("defaultWhiteListMap.json");
   fileContent = CharStreams.toString(new InputStreamReader(byteContent, StandardCharsets.UTF_8));
  } else {
   fileContent = Files.asCharSource(new File(mapPath), StandardCharsets.UTF_8).read();
  }
  return mapper.readerFor(new TypeReference<ImmutableSortedMap<String, ImmutableList<String>>>()
  {
  }).readValue(fileContent);
 }
 catch (IOException e) {
  throw new ISE(e, "Got an exception while parsing file [%s]", actualPath);
 }
}

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

@Override
public CharSource createSource(String string) throws IOException {
 checkNotNull(string);
 File file = createFile();
 Writer writer = new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_8);
 try {
  writer.write(string);
 } finally {
  writer.close();
 }
 return Files.asCharSource(file, Charsets.UTF_8);
}

代码示例来源:origin: apache/incubator-druid

private ImmutableSortedMap<String, ImmutableSet<String>> readMap(final String mapPath)
 {
  String fileContent;
  String actualPath = mapPath;
  try {
   if (Strings.isNullOrEmpty(mapPath)) {
    URL resource = this.getClass().getClassLoader().getResource("defaultWhiteListMap.json");
    actualPath = resource.getFile();
    LOGGER.info("using default whiteList map located at [%s]", actualPath);
    fileContent = Resources.toString(resource, Charset.defaultCharset());
   } else {
    fileContent = Files.asCharSource(new File(mapPath), Charset.forName("UTF-8")).read();
   }
   return mapper.readerFor(new TypeReference<ImmutableSortedMap<String, ImmutableSet<String>>>()
   {
   }).readValue(fileContent);
  }
  catch (IOException e) {
   throw new ISE(e, "Got an exception while parsing file [%s]", actualPath);
  }
 }
}

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

/**
 * Streams lines from a {@link File}, stopping when our callback returns false, or we have read
 * all of the lines.
 *
 * @param file the file to read from
 * @param charset the charset used to decode the input stream; see {@link StandardCharsets} for
 *     helpful predefined constants
 * @param callback the {@link LineProcessor} to use to handle the lines
 * @return the output of processing the lines
 * @throws IOException if an I/O error occurs
 * @deprecated Prefer {@code asCharSource(file, charset).readLines(callback)}. This method is
 *     scheduled to be removed in January 2019.
 */
@Deprecated
@CanIgnoreReturnValue // some processors won't return a useful result
public static <T> T readLines(File file, Charset charset, LineProcessor<T> callback)
  throws IOException {
 return asCharSource(file, charset).readLines(callback);
}

代码示例来源:origin: apache/incubator-druid

public static final String readFileFromClasspathAsString(String fileName) throws IOException
 {
  return Files.asCharSource(
    new File(SketchAggregationTest.class.getClassLoader().getResource(fileName).getFile()),
    Charset.forName("UTF-8")
  ).read();
 }
}

代码示例来源:origin: apache/incubator-druid

public static final String readFileFromClasspathAsString(String fileName) throws IOException
 {
  return Files.asCharSource(
    new File(SketchAggregationTest.class.getClassLoader().getResource(fileName).getFile()),
    Charset.forName("UTF-8")
  ).read();
 }
}

代码示例来源:origin: apache/incubator-druid

public static final String readFileFromClasspathAsString(String fileName) throws IOException
 {
  return Files.asCharSource(
    new File(OldApiSketchAggregationTest.class.getClassLoader().getResource(fileName).getFile()),
    Charset.forName("UTF-8")
  ).read();
 }
}

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

private static LoadedKey loadKeyFile(File file)
{
  if (!file.canRead()) {
    throw new SignatureException("Unknown signing key ID");
  }
  // try to load the key as a PEM encoded public key
  try {
    return new LoadedKey(PemReader.loadPublicKey(file));
  }
  catch (Exception ignored) {
  }
  // try to load the key as a base64 encoded HMAC key
  try {
    String base64Key = asCharSource(file, US_ASCII).read();
    byte[] rawKey = getMimeDecoder().decode(base64Key.getBytes(US_ASCII));
    return new LoadedKey(rawKey);
  }
  catch (IOException ignored) {
  }
  throw new SignatureException("Unknown signing key id");
}

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

@BeforeClass
public void setup()
    throws Exception
{
  Logging.initialize();
  URL resource = getClass().getClassLoader().getResource("33.privateKey");
  assertNotNull(resource, "key directory not found");
  File keyDir = new File(resource.getFile()).getAbsoluteFile().getParentFile();
  defaultKey = getMimeDecoder().decode(asCharSource(new File(keyDir, "default-key.key"), US_ASCII).read().getBytes(US_ASCII));
  hmac222 = getMimeDecoder().decode(asCharSource(new File(keyDir, "222.key"), US_ASCII).read().getBytes(US_ASCII));
  privateKey33 = PemReader.loadPrivateKey(new File(keyDir, "33.privateKey"), Optional.empty());
  server = new TestingPrestoServer(
      true,
      ImmutableMap.<String, String>builder()
          .put("http-server.authentication.type", "JWT")
          .put("http.authentication.jwt.key-file", new File(keyDir, "${KID}.key").toString())
          .put("http-server.https.enabled", "true")
          .put("http-server.https.keystore.path", getResource("localhost.keystore").getPath())
          .put("http-server.https.keystore.key", "changeit")
          .build(),
      null,
      null,
      new SqlParserOptions(),
      ImmutableList.of());
  server.installPlugin(new TpchPlugin());
  server.createCatalog(TEST_CATALOG, "tpch");
  waitForNodeRefresh(server);
}

相关文章