Javafx refresh src folder -
i wrote program each user has account, , can change head portrait. when sign in, directory "src/username/password" created, , image "src/username/password/headportrait.jpg" created. default headportrait expected show on label, problem occurs: image added "src/username/password" directory, src folder in eclipse not refreshed, program can't find added image , throws exception. so, must exit program ,refresh src folder , run program again. that's absolutely not expect. should that? here important part of code:
string username=name.gettext(); //"name" textfield string password=word.gettext(); //"word" textfield file namefile=new file("src/"+username); file passwordfile=new file("src/"+username+"/"+password); if(namefile.mkdirs()){ if(passwordfile.mkdirs()){ file default=new file("src/images/headportrait.jpg");//the default fead portrait put under directory. try{ fileinputstream in=new fileinputstream(default); fileoutputstream out=new fileoutputstream("src/"+username+"/"+password+"/"+"headportrait.jpg"); bufferedinputstream bufferedin=new bufferedinputstream(in); bufferedoutputstream bufferedout=new bufferedoutputstream(out); byte[] bytes=new byte[1]; while(bufferedin.read(bytes)!=-1){ bufferedout.write(bytes); } bufferedout.flush(); bufferedin.close(); bufferedout.close(); }catch(ioexception e){ e.printstacktrace(); } //here, problem aready occurs! folders , default //portrait created src folder in eclipse //is not refreshed, default portrait won't show up! }else{ //show failure message } }else{ //show failure message } label portrait=new label(); imageview userimage=new imageview(new image(this.getclass.getresouceasstream("/"+username+"/"+password+"/"+headportrait.jpg))); portrait.setgraphic(userimage); //and userimage won't show , throws exception, because src folder in eclipse not refreshed!
this not approach should use since work in development environment. when deploy app compiled code contained in .jar
archive jvm accesses.
the way go store data in convenient place (server, user directory, ...) , load source.
example (user directory)
static path appdirectory = new file(system.getproperty("user.home")).topath().resolve("myapp"); static void copy(inputstream in, outputstream out) throws ioexception { bufferedinputstream bufferedin=new bufferedinputstream(in); bufferedoutputstream bufferedout=new bufferedoutputstream(out); byte[] bytes = new byte[1024]; while(bufferedin.read(bytes)!=-1){ bufferedout.write(bytes); } }
string username=name.gettext(); //"name" textfield string password=word.gettext(); //"word" textfield path passworddirectory = appdirectory.resolve(paths.get(username, password)); path userimage = passworddirectory.resolve("headportrait.jpg"); files.createdirectories(passworddirectory); try (inputstream in = getclass().getresourceasstream("/images/headportrait.jpg"); // make sure image included resource outputstream out = files.newoutputstream(userimage)) { copy(in, out); }
imageview userimageview = new imageview(new image(userimage.touri().tostring()));
btw: creating directory user name , password seems bad idea me. password may contain characters must not part of file name after all... require move files when the password changed... not mention password stored plain text in prominent place...
Comments
Post a Comment