Class Resources
java.lang.Object
com.google.common.io.Resources
Provides utility methods for working with resources in the classpath. Note that even though these
methods use
URL
parameters, they are usually not appropriate for HTTP or other
non-classpath resources.- Since:
- 1.0
- Author:
- Chris Nokleberg, Ben Yu, Colin Decker
-
Method Summary
Modifier and TypeMethodDescriptionstatic 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, OutputStream to) Copies all bytes from a URL to an output stream.static URL
getResource
(Class<?> contextClass, String resourceName) Given aresourceName
that is relative tocontextClass
, returns aURL
pointing to the named resource.static URL
getResource
(String resourceName) Reads all of the lines from a URL.readLines
(URL url, Charset charset, LineProcessor<T> callback) Streams lines from a URL, stopping when our callback returns false, or we have read all of the lines.static byte[]
toByteArray
(URL url) Reads all bytes from a URL into a byte array.static String
Reads all characters from a URL into aString
, using the given character set.
-
Method Details
-
asByteSource
Returns aByteSource
that reads from the given URL.- Since:
- 14.0
-
asCharSource
Returns aCharSource
that reads from the given URL using the given character set.- Since:
- 14.0
-
toByteArray
Reads all bytes from a URL into a byte array.- Parameters:
url
- the URL to read from- Returns:
- a byte array containing all the bytes from the URL
- Throws:
IOException
- if an I/O error occurs
-
toString
Reads all characters from a URL into aString
, using the given character set.- Parameters:
url
- the URL to read fromcharset
- the charset used to decode the input stream; seeStandardCharsets
for helpful predefined constants- Returns:
- a string containing all the characters from the URL
- Throws:
IOException
- if an I/O error occurs.
-
readLines
@CanIgnoreReturnValue public static <T extends @Nullable Object> T readLines(URL url, Charset charset, LineProcessor<T> callback) throws IOException Streams lines from a URL, stopping when our callback returns false, or we have read all of the lines.- Parameters:
url
- the URL to read fromcharset
- the charset used to decode the input stream; seeStandardCharsets
for helpful predefined constantscallback
- the LineProcessor to use to handle the lines- Returns:
- the output of processing the lines
- Throws:
IOException
- if an I/O error occurs
-
readLines
Reads all of the lines from a URL. The lines do not include line-termination characters, but do include other leading and trailing whitespace.This method returns a mutable
List
. For anImmutableList
, useResources.asCharSource(url, charset).readLines()
.- Parameters:
url
- the URL to read fromcharset
- the charset used to decode the input stream; seeStandardCharsets
for helpful predefined constants- Returns:
- a mutable
List
containing all the lines - Throws:
IOException
- if an I/O error occurs
-
copy
Copies all bytes from a URL to an output stream.- Parameters:
from
- the URL to read fromto
- the output stream- Throws:
IOException
- if an I/O error occurs
-
getResource
Returns aURL
pointing toresourceName
if the resource is found using the context class loader. In simple environments, the context class loader will find resources from the class path. In environments where different threads can have different class loaders, for example app servers, the context class loader will typically have been set to an appropriate loader for the current thread.In the unusual case where the context class loader is null, the class loader that loaded this class (
Resources
) will be used instead.- Throws:
IllegalArgumentException
- if the resource is not found
-
getResource
Given aresourceName
that is relative tocontextClass
, returns aURL
pointing to the named resource.- Throws:
IllegalArgumentException
- if the resource is not found
-