Resources 提供提供操作 classpath 路径下所有资源的方法 除非另有说明, 否则类中所有方法的参数都不能为 null 虽然有些方法的参数是 URL 类型的, 但是这些方法实现通常不是以 HTTP 完成的 ; 同时这些资源也非 classpath 路径下的 下面两个函数都是根据资源的名称得到其绝对路径, 从函数里面可以看出,Resources 类中的 getresource 函数都是基于 java 中 ClassLoader 类的 getresource 函数来实现的, 只是 Resources 类中的 getresource 函数给我们封装了 ClassLoader 对象的获取, 使得用户不需要自己去获取 Class Loader 对象 public static URL getresource(string resourcename) { ClassLoader loader = Objects.firstNonNull( Thread.currentThread().getContextClassLoader(), Resources.class.getClassLoader()); URL url = loader.getresource(resourcename); checkargument(url!= null, "resource %s not found.", resourcename); return url; public static URL getresource(class<?> contextclass, String resourcename) { URL url = contextclass.getresource(resourcename); checkargument(url!= null, "resource %s relative to %s not found.", resourcename, contextclass.getname()); return url; 上面两个函数处理完后都是返回 URL 类型的对象, 第一个 getresource 函数中 Objects.firstNo nnull 函数原型为 public static <T> T firstnonnull(@nullable T first, @Nullable T second) 其实现如下 : public static <T> T firstnonnull(@nullable T first, @Nullable T second) { return first!= null? first : checknotnull(second); 1 / 5
从其实现可以看出,firstNonNull 函数返回其两个参数中首先不为 null 的对象 ; 如若两个参数都为 null, 则该函数将会抛出类型为 NullPointerException 异常 好了, 再回到 Resources 类中来, 上面的两个 getresource 函数返回的对象都是 URL 类型的, 而观察 Resources 类其他函数, 都可以接受 URL 类型的参数, 如下所示 : public static InputSupplier<InputStream> newinputstreamsupplier(url url) public static ByteSource asbytesource(url url) public static InputSupplier<InputStreamReader> newreadersupplier (URL url, Charset charset) public static CharSource ascharsource(url url, Charset charset) public static byte[] tobytearray(url url) throws IOException public static String tostring(url url, Charset charset) throws IOException public static <T> T readlines(url url, Charset charset, LineProcessor<T> callback) throws IOException public static List<String> readlines(url url, Charset charset) throws IOException public static void copy(url from, OutputStream to) throws IOException 下面分别介绍这些函数的用法 : InputSupplier<InputStream> inputstreaminputsupplier = Resources.newInputStreamSupplier(resource); InputStream input = inputstreaminputsupplier.getinput(); ByteSource bytesource = Resources.asByteSource(resource); InputStream inputstream = bytesource.openstream(); newinputstreamsupplier 和 asbytesource 函数都是以字节形式来读取 resource 中的数据 可以看出, 两个函数最后都可以转换成我们熟悉的 InputStream 来操作 InputSupplier<InputStreamReader> inputsrs = Resources.newReaderSupplier(resource, Charsets.UTF_8); Reader input1 = inputsrs.getinput(); CharSource charsource = Resources.asCharSource(resource, Charsets.UTF_8); Reader reader = charsource.openstream(); 2 / 5
newreadersupplier 和 ascharsource 函数都是以字符的形式来读取 resource 中的数据 同上面两个函数一样, 这里说的两个函数都可以转换为 Reader 类操作 byte[] bytes = Resources.toByteArray(resource); String string = Resources.toString(resource, Charsets.UTF_8); 上面两个函数可以直接将 resource 中的资源数据转换为字节数组和字符数组形式 List<String> stringlist = Resources.readLines(resource, Charsets.UTF_8); Lines lines = (Lines)Resources.readLines(resource, Charsets.UTF_8, new LineProcessor<Object>() { Lines lines = new Lines(); public boolean processline(string line) throws IOException { return lines.getstringlist().add(line); ); public Lines getresult() { return lines; Lines 类定义 public class Lines { private List<String> stringlist; public Lines() { stringlist = Lists.newArrayList(); public List<String> getstringlist() { return stringlist; public void setstringlist(list<string> stringlist) { this.stringlist = stringlist; 3 / 5
public String tostring() { return stringlist.tostring(); 上面两个函数都是从 resource 中以 Charsets.UTF_8 字符集形式一行一行的读取里面的内容 第二个函数我们自己实现了 LineProcessor callback 类, 可以看出, 我们可以对读取到的每一个进行相应的处理, 非常的方便 其实,Resources.readLines(resource, Charsets.UTF_8); 函数内部还是调用了 readlines(url url, Charset charset, LineProcessor callback) 函数, 其实现如下 : public static List<String> readlines(url url, Charset charset) throws IOException { return readlines(url, charset, new LineProcessor<List<String>>() { final List<String> result = Lists.newArrayList(); public boolean processline(string line) { result.add(line); return true; public List<String> getresult() { return result; ); 最后一个要说的函数是 copy(url from, OutputStream to), 这个函数可以将数据复制到所有 OutputStream 子类对象中, 如下 : Resources.copy(resource, new FileOutputStream("/wyp.txt")); 上面代码将 resource 中的数据复制到 wyp.txt 文件中 ( 完 )
Powered by TCPDF (www.tcpdf.org) 本博客文章除特别声明, 全部都是原创! 转载本文请加上 : 转载自过往记忆 (https://www.iteblog.com/) 本文链接 : () 5 / 5