I have a number of resource files I want to copy to another location as a method call. There are various how-tos and guides, and questions here on SO about how to read/copy files from a JAR. However if I am not completely off, the standard ways of getting a resource will be looking at different places when the code is run from a deployed JAR, compared to when the code is run from Eclipse while developing/debugging. So the solution that works for one case doesn't for the other..
My current and extremely verbose solution (see below) works, but it's ugly, long, and annoying to maintain (to add or remove an additional file). Is there way I can optimise this method, (possibly with new voodoo from NIO package) for instance by just loop over the files in the /res
folder and just copy them all to the new location. I'm not sure how to avoid specifying the filenames and paths for getResource()
would work in that scenario.
private void loadRes() throws IOException{
// get styling resources from the jar file
File htmlRes = new File(topfolder, "res");
if(!htmlRes.exists() || !htmlRes.isDirectory())
htmlRes.mkdir();
File cssfile = new File(htmlRes, "style.css"),
bullet_file = new File(htmlRes,"i.gif"),
banner_file = new File(htmlRes, "banner.png"),
bg_file = new File(htmlRes,"body_bg.png"),
img_bg = new File(htmlRes,"button-bg.png"),
hov_bg = new File(htmlRes,"button-hover-bg.png"),
visu_img = new File(htmlRes,"visu.png"),
pc_img = new File(htmlRes,"pc.png"),
asc_gif = new File(htmlRes,"asc.gif"),
desc_gif = new File(htmlRes,"desc.gif"),
bg_gif = new File(htmlRes,"bg.gif");
URL css_url = ExportUtils.class.getResource("/res/style.css");
URL bullet_url = ExportUtils.class.getResource("/res/i.gif");
URL banner_url = ExportUtils.class.getResource("/res/banner.png");
URL bg_url = ExportUtils.class.getResource("/res/body_bg.png");
URL image1_url = ExportUtils.class.getResource("/res/button-bg.png");
URL image2_url = ExportUtils.class.getResource("/res/button-hover-bg.png");
URL visu_url = ExportUtils.class.getResource("/res/visu.png");
URL pc_url = ExportUtils.class.getResource("/res/pc.png");
URL asc_url = ExportUtils.class.getResource("/res/asc.gif");
URL desc_url = ExportUtils.class.getResource("/res/desc.gif");
URL bggif_url = ExportUtils.class.getResource("/res/bg.gif");
FileOutputStream fos = new FileOutputStream(cssfile);
Resources.copy(css_url, fos);
fos = new FileOutputStream(bullet_file);
Resources.copy(bullet_url, fos);
fos = new FileOutputStream(banner_file);
Resources.copy(banner_url, fos);
fos = new FileOutputStream(bg_file);
Resources.copy(bg_url, fos);
fos = new FileOutputStream(img_bg);
Resources.copy(image1_url, fos);
fos = new FileOutputStream(hov_bg);
Resources.copy(image2_url, fos);
fos = new FileOutputStream(visu_img);
Resources.copy(visu_url, fos);
fos = new FileOutputStream(pc_img);
Resources.copy(pc_url, fos);
fos = new FileOutputStream(asc_gif);
Resources.copy(asc_url, fos);
fos = new FileOutputStream(desc_gif);
Resources.copy(desc_url, fos);
fos = new FileOutputStream(bg_gif);
Resources.copy(bggif_url, fos);
// scripts and finish head
URL sort_script = ExportUtils.class.getResource("/res/scripts.js");
URL func_script = ExportUtils.class.getResource("/res/jquery.tablesorter.min.js");
URL scinot_parser = ExportUtils.class.getResource("/res/jquery.tablesorter.scinot.js");
File div_js = new File(htmlRes,"scripts.js");
File jquery_sorttable = new File(htmlRes,"jquery.tablesorter.min.js");
File jquery_st_scinot = new File(htmlRes, "jquery.tablesorter.scinot.js");
fos = new FileOutputStream(div_js);
Resources.copy(sort_script, fos);
fos = new FileOutputStream(jquery_sorttable);
Resources.copy(func_script, fos);
fos = new FileOutputStream(jquery_st_scinot);
Resources.copy(scinot_parser, fos);
}