2013-12-13 41 views
0

我訪問字體資源使用下面的代碼我可以使用File對象訪問資源嗎?

 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

回答

0

我可以訪問文件對象的資源?

不,他們沒有文件了。

如何枚舉裏面JAR資源?可以使用File嗎?

每當有人問這個問題,我想知道他們爲什麼要在地球上這樣做。在構建Jar時,創建一個資源列表並將其放入Jar中的「已知位置」。在運行時,得到清單,解析它,然後從那裏開始。

+0

當然我會硬編碼一個列表,如果它不可能產生它。在我的情況下,這些是字體,很容易添加新的字體。 –

+0

有沒有要發表評論?如果是這樣,它就會丟失在我身上。 –

相關問題