一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

java 读取jar包中的资源文件实例程序

时间:2013-09-17 编辑:简简单单 来源:一聚教程网

1) ClassLoader是abstract的,不可能实例化对象,更加不可能通过ClassLoader调用上面两个方法。所以我们真正写代码的时候,是通过Class类中的getResource()和getResourceAsStream()方法,这两个方法会委托ClassLoader中的getResource()和getResourceAsStream()方法 。好了,现在我们重新写一段Resource代码,来看看上面那段费解的话是什么意思了:

Java代码

 代码如下 复制代码

package edu.hxraid; 
   import java.io.*; 
    import java.net.URL; 
    public class Resource { 
       public  void getResource() throws IOException{   
              //查找指定资源的URL,其中res.txt仍然开始的bin目录下  
         URL fileURL=this.getClass().getResource("/resource/res.txt");  
          System.out.println(fileURL.getFile()); 
     } 
      public static void main(String[] args) throws IOException { 
         Resource res=new Resource(); 
         res.getResource(); 
    } 
   }

(2) 我们不能用常规操作文件的方法来读取ResourceJar.jar中的资源文件res.txt,但可以通过Class类的getResourceAsStream()方法来获取 ,这种方法是如何读取jar中的资源文件的,这一点对于我们来说是透明的。我们将Resource.java改写成:

Java代码

 代码如下 复制代码

package edu.hxraid; 
    import java.io.*; 
   public class Resource { 
     public void getResource() throws IOException{ 
      //返回读取指定资源的输入流 
          InputStream is=this.getClass().getResourceAsStream("/resource/res.txt");  
        BufferedReader br=new BufferedReader(new InputStreamReader(is)); 
       String s=""; 
         while((s=br.readLine())!=null) 
             System.out.println(s); 
     } 
 }

注意

一般我们在编写完Java程序中获取资源路径,在开发和调试期(未打包前),是没有问题的,但在打包后,由于资源(图片、配置文件等)都将打包到jar文件中,由于System中的“user.dir”属性发生了变化,会造成用绝对路径的方式无法找到jar文件中的资源文件。因为程序载入图片或文本文件时,使用以当前工作路径为基准的方式来指定文件和路径,而资源文件打在jar包中后程序无法通过绝对路径找到。因此可采用Java本身以类为基准的路径搜索方式。如下两种方式:

取得相对于包的根路径

 

 代码如下 复制代码

String path = new File(FrameConfig.class.getResource("/").getFile()) 

.getAbsolutePath();

取得相对于包路径的流

Reader reader = new InputStreamReader( 

FrameConfig.class.getResourceAsStream("/res/uiConfig.xml"));

含有这种代码的程序在运行时,以类(类路径)为基准,而不依赖当前路径(System中的user.dir),

【注】:上面的路径“/res/uiConfig.xml”是相对于包的路径,如果写成“res/uiConfig.xml”,表示相对于类的路径:package/res/uiConfig.xml,这需要将图片和文本等文件的保存路径,和程序中指定的路径两者保持一致。

【注】:上面的路径“/res/uiConfig.xml”是相对于包的路径,如果写成“res/uiConfig.xml”,表示相对于类的路径:package/res/uiConfig.xml,这需要将图片和文本等文件的保存路径,和程序中指定的路径两者保持一致。

读取jar包中根Element(dom4j)

 代码如下 复制代码

private static Element getRootElement(String path) {
 Element rootElement = null;
 Reader reader = null;
 try {
  reader = new InputStreamReader(
    FrameConfig.class.getResourceAsStream(path));
  rootElement = new SAXReader().read(reader).getRootElement();
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  closeStream(reader);
 }
 return rootElement;
}

private static void closeStream(Closeable stream) {
 if (stream != null)
  try {
   stream.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
}

public static void main(String[] args) {
 Element uiConfig = getRootElement("/res/uiConfig.xml");
}


读取jar包中根Element(jdom)

 代码如下 复制代码

private static Element getRootElement(String path) {
 Element rootElement = null;
 Reader reader = null;
 try {
  reader = new InputStreamReader(
    FrameConfig.class.getResourceAsStream(path));
  rootElement = new SAXBuilder().build(reader).getRootElement();
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  closeStream(reader);
 }
 return rootElement;
}


读取jar包中国际化资源文件

 代码如下 复制代码

ResourceBundle resources = ResourceBundle.getBundle(
  "res.i18n", Locale.getDefault());
// 取得对应的值
String value = resources.getString(key);


读取jar包中的图片资源

 代码如下 复制代码

public static Image getImage(String imageName) {
 InputStream inputStream = null;
 ByteArrayOutputStream outputStream = null;
 try {
  inputStream = FrameConfig.class.getResourceAsStream("/res/images/"
    + imageName);
  outputStream = new ByteArrayOutputStream();
  byte buffer[] = new byte[1024];
  int len = 0;
  while ((len = inputStream.read(buffer)) != -1)
   outputStream.write(buffer, 0, len);
  return Toolkit.getDefaultToolkit().createImage(
    outputStream.toByteArray());
 } catch (Throwable th) {
  th.printStackTrace();
 } finally {
  closeStream(inputStream);
  closeStream(outputStream);
 }
 return null;
}

public static void main(String[] args) {
 Image image = getImage("add.gif");
}

热门栏目