【发布时间】:2013-12-31 21:56:57
【问题描述】:
我正在使用以下代码访问字体资源
try {
URL url = getClass().getResource("");
log.debug("url = '{}'", url.toString());
URI uri = url.toURI();
log.debug("uri = '{}'", uri.toString());
File dir = new File(uri);
files = dir.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".ttf");
}
});
indice = new Integer[files.length];
delegate = new Descr[files.length];
fileNames = new String[files.length];
fontNames = new String[files.length];
for(int i=0; i<files.length; ++i) {
delegate[i] = new Descr(Font.createFont(Font.TRUETYPE_FONT, files[i]).deriveFont(FontSize));
fileNames[i] = files[i].getName();
fontNames[i] = delegate[i].getFont().getFontName();
}
} catch (URISyntaxException e) {
throw new AssertionError(e);
} catch (FontFormatException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
在放入 JAR 之前它一直在工作。在JAR 中会导致错误
IllegalargumentException: URI is not hierarchicalhierarchical
那么,如何枚举JAR内部的资源呢?可以使用File吗?
【问题讨论】:
标签: java file fonts jar embedded-resource