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

x33g5p2x  于2022-01-28 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(105)

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

Resources.toString介绍

[英]Reads all characters from a URL into a String, using the given character set.
[中]使用给定的字符集将URL中的所有字符读入字符串。

代码示例

代码示例来源:origin: springside/springside4

/**
 * 读取文件的每一行,读取规则见本类注释.
 */
public static String toString(String resourceName) throws IOException {
  return Resources.toString(Resources.getResource(resourceName), Charsets.UTF_8);
}

代码示例来源:origin: springside/springside4

/**
 * 读取文件的每一行,读取规则见本类注释.
 */
public static String toString(Class<?> contextClass, String resourceName) throws IOException {
  return Resources.toString(Resources.getResource(contextClass, resourceName), Charsets.UTF_8);
}

代码示例来源:origin: Graylog2/graylog2-server

@Inject
public GraylogErrorPageGenerator(Engine templateEngine) throws IOException {
  this(Resources.toString(Resources.getResource("error.html.template"), StandardCharsets.UTF_8), templateEngine);
}

代码示例来源:origin: spotify/helios

private void logBanner() {
 try {
  final String banner = Resources.toString(Resources.getResource("agent-banner.txt"), UTF_8);
  log.info("\n{}", banner);
 } catch (IllegalArgumentException | IOException ignored) {
  // ignore
 }
}

代码示例来源:origin: spotify/helios

private void logBanner() {
 try {
  final String banner = Resources.toString(Resources.getResource("master-banner.txt"), UTF_8);
  log.info("\n{}", banner);
 } catch (IllegalArgumentException | IOException ignored) {
  // ignored
 }
}

代码示例来源:origin: apache/usergrid

/**
 * Get the content from our mappings file
 * @return
 */
private String getMappingsContent(){
  URL url = Resources.getResource("org/apache/usergrid/persistence/index/usergrid-mappings.json");
  try {
    return Resources.toString(url, Charsets.UTF_8);
  }
  catch ( IOException e ) {
    throw new RuntimeException( "Unable to read mappings file", e );
  }
}

代码示例来源:origin: apache/usergrid

/**
 * Get the content from our mappings file
 * @return
 */
private String getMappingsContent(){
  URL url = Resources.getResource("org/apache/usergrid/persistence/index/usergrid-mappings.json");
  try {
    return Resources.toString(url, Charsets.UTF_8);
  }
  catch ( IOException e ) {
    throw new RuntimeException( "Unable to read mappings file", e );
  }
}

代码示例来源:origin: OryxProject/oryx

@PostConstruct
public final void loadHTML() throws IOException {
 StringBuilder htmlBuilder = new StringBuilder(10000);
 for (String resource : new String[] { "console-header.html.fragment",
                    getConsoleResource(),
                    "console-footer.html.fragment" }) {
  URL resourceURL = Resources.getResource(AbstractConsoleResource.class, resource);
  htmlBuilder.append(Resources.toString(resourceURL, StandardCharsets.UTF_8));
 }
 html = htmlBuilder.toString();
}

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

private static String readResource(String name)
    throws IOException
{
  return Resources.toString(Resources.getResource(name), UTF_8);
}

代码示例来源:origin: Graylog2/graylog2-server

@Inject
public IndexHtmlGenerator(final PluginAssets pluginAssets,
             final Engine templateEngine,
             final HttpConfiguration httpConfiguration) throws IOException {
  this(
      Resources.toString(Resources.getResource("web-interface/index.html.template"), StandardCharsets.UTF_8),
      pluginAssets.cssFiles(),
      pluginAssets.sortedJsFiles(),
      templateEngine,
      httpConfiguration);
}

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

private static String resourceAsString(String resourcePath)
{
  try {
    URL resourceUrl = Resources.getResource(resourcePath);
    return Resources.toString(resourceUrl, UTF_8);
  }
  catch (IOException e) {
    throw new RuntimeException(e);
  }
}

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

private static String read(String resource)
{
  try {
    return Resources.toString(getResource(AbstractCostBasedPlanTest.class, resource), UTF_8);
  }
  catch (IOException e) {
    throw new UncheckedIOException(e);
  }
}

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

public static String readResource(String resource)
  {
    try {
      return Resources.toString(getResource(resource), UTF_8);
    }
    catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }
}

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

private static String prepareCassandraYaml()
    throws IOException
{
  String original = Resources.toString(getResource("cu-cassandra.yaml"), UTF_8);
  File tempDirFile = createTempDir();
  tempDirFile.deleteOnExit();
  Path tmpDirPath = tempDirFile.toPath();
  Path dataDir = tmpDirPath.resolve("data");
  Files.createDirectory(dataDir);
  String modified = original.replaceAll("\\$\\{data_directory\\}", dataDir.toAbsolutePath().toString());
  Path yamlLocation = tmpDirPath.resolve("cu-cassandra.yaml");
  write(modified, yamlLocation.toFile(), UTF_8);
  return yamlLocation.toAbsolutePath().toString();
}

代码示例来源:origin: kiegroup/optaplanner

protected static String readResourceToString(String resourceString) throws IOException {
  URL url = Resources.getResource(resourceString);
  return Resources.toString(url, Charset.forName("UTF-8"));
}

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

public void testGetResource_contextClassLoader() throws IOException {
 // Check that we can find a resource if it is visible to the context class
 // loader, even if it is not visible to the loader of the Resources class.
 File tempFile = createTempFile();
 PrintWriter writer = new PrintWriter(tempFile, "UTF-8");
 writer.println("rud a chur ar an méar fhada");
 writer.close();
 // First check that we can't find it without setting the context loader.
 // This is a sanity check that the test doesn't spuriously pass because
 // the resource is visible to the system class loader.
 try {
  Resources.getResource(tempFile.getName());
  fail("Should get IllegalArgumentException");
 } catch (IllegalArgumentException expected) {
 }
 // Now set the context loader to one that should find the resource.
 URL baseUrl = tempFile.getParentFile().toURI().toURL();
 URLClassLoader loader = new URLClassLoader(new URL[] {baseUrl});
 ClassLoader oldContextLoader = Thread.currentThread().getContextClassLoader();
 try {
  Thread.currentThread().setContextClassLoader(loader);
  URL url = Resources.getResource(tempFile.getName());
  String text = Resources.toString(url, Charsets.UTF_8);
  assertEquals("rud a chur ar an méar fhada\n", text);
 } finally {
  Thread.currentThread().setContextClassLoader(oldContextLoader);
 }
}

代码示例来源:origin: soabase/exhibitor

try
  resource = Resources.getResource("com/netflix/exhibitor/core/ui/" + fileName);
if ( contentType.startsWith("text/") )
  entity = Resources.toString(resource, Charset.forName("UTF-8"));

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

/**
 * Load a file containing a serialized thrift message in from disk.
 */
@Before
public void setUp() throws IOException {
  fileContents = Resources.toString(
      Resources.getResource(
          getClass(),
          "/com/linecorp/armeria/common/thrift/text/TTextProtocol_TestData.txt"),
      Charsets.UTF_8);
  base64Encoder = new Base64();
}

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

@Override
protected List<JSONObject> getChildren() {
 try {
  List<JSONObject> children = new ArrayList<>();
  String jsonStr =
    Resources.toString(
      Resources.getResource(this.getClass(), "read_tests.json"), StandardCharsets.UTF_8);
  JSONObject json = new JSONObject(jsonStr);
  JSONArray testCases = json.getJSONArray("tests");
  for (int i = 0; i < testCases.length(); i++) {
   JSONObject testCase = testCases.getJSONObject(i);
   children.add(testCase);
  }
  return children;
 } catch (Exception e) {
  throw new IllegalStateException(e);
 }
}

代码示例来源:origin: testcontainers/testcontainers-java

@Test
  public void testSplit() throws IOException {
    final String script = Resources.toString(Resources.getResource("splittable.sql"), Charsets.UTF_8);

    final List<String> statements = new ArrayList<>();
    ScriptUtils.splitSqlScript("resourcename", script, ";", "--", "/*", "*/", statements);

    assertEquals(7, statements.size());
    assertEquals("SELECT \"a /* string literal containing comment characters like -- here\"", statements.get(2));
    assertEquals("SELECT \"a 'quoting' \\\"scenario ` involving BEGIN keyword\\\" here\"", statements.get(3));
    assertEquals("SELECT * from `bar`", statements.get(4));
    assertEquals("INSERT INTO bar (foo) VALUES ('hello world')", statements.get(6));
  }
}

相关文章