假设 classpath 中有个文件是: hello.txt
guava 读取其中的文件内容:
URL url = Resources.getResource("hello.txt");
List<String> lines = Resources.asCharSource(url, Charsets.UTF_8).readLines();
读取 http://www.iteye.com/ 的内容:
URL url = new URL("http://www.iteye.com/");
List<String> lines = Resources.asCharSource(url, Charsets.UTF_8).readLines();
下面是 Guava 中Resources的官网 API。
==============
com.google.common.io
Class Resources
- java.lang.Object
-
- com.google.common.io.Resources
-
@Beta @GwtIncompatible public final class Resources extends Object
Provides utility methods for working with resources in the classpath. Note that even though these methods useURL
parameters, they are usually not appropriate for HTTP or other non-classpath resources.All method parameters must be non-null unless documented otherwise.
-
Since:
- 1.0 Author:
- Chris Nokleberg, Ben Yu, Colin Decker
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method and Description static ByteSource
asByteSource(URL url)
Returns aByteSource
that reads from the given URL.static CharSource
asCharSource(URL url, Charset charset)
Returns aCharSource
that reads from the given URL using the given character set.static void
copy(URL from,
-