Im working on a little programm, which should do the annoying copy&paste stuff for me. So i've written a code that allows me to insert the right titles, numbers etc. and that would then copy my inputs in 4 different files at the right place. I have prepared some files (.xml and .html) so that editing is easier. Im using a keyword, and then .replaceAll("", "");
My plan is to keep the prepared files as a resource, so i could drag my .jar anywhere on my computer an let the programm generate new files from my resources and also edit files that are in the same directory as my .jar.
Okay so whenever i launch my application via eclipse, everything is fine. The programm creates new files based on my inputs using my templates, and edits the existing files i chose to be edited. But when i export everything into a .jar the programm stops at a point, without an error or anything like that.
jarpath = getClass().getProtectionDomain().getCodeSource()
.getLocation().getPath().substring(1);
//substring removes a /
jarpathparent = jarpath.replace("alpha.jar", "");
// replacing with "" to get the only the directory where the .jar is.
public void generate(String s) throws IOException {
String path = "", content = "";
File file = null, outputfile = null;
switch (s) {
case "index":
Dialog.info("", "1");
path = getClass().getResource("index.html").toString()
.replace("file:/", "");
Dialog.info("", "2");
file = new File(path);
Dialog.info("", "3");
outputfile = new File(jarpathparent + "/index.html");
Dialog.info("", "4");
content = FileUtils.readFileToString(file);
Dialog.info("", "5");
content = content.replaceAll("XforTitle", title);
Dialog.info("", "6");
FileUtils.writeStringToFile(outputfile, content);
Dialog.info("", "7");
break;
case "index2":
.
.
.
I have used a little dialog-class i created, to spot where the programm stops. The fourth dialogbox is the last one thats popping up, so the problem seem so be
content = FileUtils.readFileToString(file);
I dont know exactly whats wrong, but maybe it's because i use apache-commons-io (FileUtils) or something like that?
And maybe a pastebin-link with the whole code: http://pastebin.com/Px7SygYu (The imagelabel and the .wav are just for amusement.)
___________EDIT________________
Okay so i can now load the resource content
by using
InputStream input = getClass().getResourceAsStream("/classpath/to/my/file");
and a code-snippet from
http://www.mkyong.com/java/how-to-convert-inputstream-to-string-in-java/
So i now have the content of the file as a string, and want to write it to a new file (in the directory, where i put my .jar)
InputStream input = getClass().getResourceAsStream("index.html");
content = getStringFromInputStream(input);
Dialog.info("", "2");
outputfile = new File(jarpathparent + "index.html");
Dialog.info("", "3");
content = content.replaceAll("XforTitle", title);
Dialog.info("", "4");
Dialog.info("", outputfile.getPath());
try {
FileUtils.writeStringToFile(outputfile, content);
} catch (Exception e) {
Dialog.vielText("", e.toString());
}
Dialog.info("", "5");
So it now crashes at FileUtils.writeStringToFile(...); but i dont det the dialog with the exception that should be caught in my try catch. i checked the outputpath, as you can see in the code, and it seems to be okay. Any other ideas on how to fix, or even how to write my string(contains the .html template) to a File ?