Runnable jar添加资源及读取

来源:互联网 发布:软件测试的含义 编辑:程序博客网 时间:2024/06/06 00:31

资源存放的位置:

打包jar没有选择打包包括具体文件夹,默认是src和lib目录;因此想要添加资源必须在src目录新建包名,然后把资源放到这个目录下;

读取资源:例子是读取包名下的资源fileTypeServer,拷贝到目录/tmp下

    public  void copyFileToTmp() {        FileOutputStream out = null;        InputStream is = null;        try {            File file = new File("/tmp/fileTypeServer");            if (!file.exists() != false) {                try {                    file.createNewFile();                } catch (IOException e) {                    e.printStackTrace();                }            }                       is = this.getClass().getResourceAsStream("fileTypeServer");            out = new FileOutputStream(file);            byte[] buf = new byte[8*1024];            int len;            while((len = is.read(buf)) != -1)            {                out.write(buf,0,len);            }            out.flush();                    } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } finally {            try {                if (is != null)                    is.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            try {                if (out != null)                    out.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }    }


0 0
原创粉丝点击