下面列出了com.google.common.io.ByteSource#asCharSource ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/***/
public static String getContentsFromFileEntry(final ZipEntry entry, String rootName) throws IOException,
URISyntaxException {
URL rootURL = Thread.currentThread().getContextClassLoader().getResource(rootName);
try (final ZipFile root = new ZipFile(new File(rootURL.toURI()));) {
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return root.getInputStream(entry);
}
};
CharSource charSrc = byteSource.asCharSource(Charsets.UTF_8);
return charSrc.read();
}
}
/**
* @param resourceName
* the classpath-relative location of the to-be-read resource
*/
private static List<String> getFileLines(final String resourceName) throws IOException {
ByteSource byteSource = new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
}
};
CharSource charSrc = byteSource.asCharSource(Charsets.UTF_8);
return charSrc.readLines();
}
public static CharSource toCharSource( final ByteSource source )
{
return source.asCharSource( Charsets.UTF_8 );
}