java - Creating files on disk from a jar -
my problem whenever run program eclipse or intellij can create many files in specific folder when create jar creates 1 file no matter how many times call method this.
in detail feature of program allows user create file in folder jar executed from. done through class containing static fields , methods. fields in class are:
private static stringbuilder sb = new stringbuilder(); private static formatter formatter = new formatter(sb); private static bufferedwriter bw; private static string filesfolderpath;
first path jar:
private static string getjarpath() throws unsupportedencodingexception { url url = printhelper.class.getprotectiondomain().getcodesource().getlocation(); string jarpath = urldecoder.decode(url.getfile(), "utf-8"); string path = new file(jarpath).getparentfile().getpath(); return path; }
then set path folder want files put in , create folder through static block in class code executed once when class first used:
static { try { string dirpath = getjarpath(); string fileseparator = system.getproperty("file.separator"); filesfolderpath = dirpath + fileseparator + "files" + fileseparator; file file = new file(filesfolderpath); file.mkdir(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } }
finally. each time user wants create file method called:
public static void print(string filename) throws ioexception, filenotfoundexception { file file = new file(filesfolderpath, getfilename(filename)); bw = new bufferedwriter(new filewriter(file)); //writing buffered writer bw.close(); }
is there reason work want when run ide , create 1 file when running jar?
Comments
Post a Comment