Javafx刷新src文件夹
我写了一个程序,每个用户都有一个帐户,他们可以更改头像。当他们登录时,将创建目录src/username/password,并且还将创建图像src/username/password/headgrait.jpg。然后,默认头像将显示在标签上,但问题出现了:图像被添加到src/username/password目录,但eclipse中的src文件夹没有刷新,因此程序无法找到添加的图像并引发异常。因此,我必须退出程序,刷新src文件夹,然后再次运行程序。这绝对不是我所期望的。我该怎么办? 以下是我的代码中最重要的部分:Javafx刷新src文件夹,javafx,refresh,Javafx,Refresh,我写了一个程序,每个用户都有一个帐户,他们可以更改头像。当他们登录时,将创建目录src/username/password,并且还将创建图像src/username/password/headgrait.jpg。然后,默认头像将显示在标签上,但问题出现了:图像被添加到src/username/password目录,但eclipse中的src文件夹没有刷新,因此程序无法找到添加的图像并引发异常。因此,我必须退出程序,刷新src文件夹,然后再次运行程序。这绝对不是我所期望的。我该怎么办? 以下是我的
String username=name.getText(); //"name" is a TextField
String password=word.getText(); //"word" is a 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 is previously put under this 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, the problem aready occurs! The folders and default
//portrait are successfully created but the src folder in eclipse
//is not refreshed, so the default portrait won't show up!
}else{
//show a failure message
}
}else{
//show a failure message
}
Label portrait=new Label();
ImageView userImage=new ImageView(new Image(this.getClass.getResouceAsStream("/"+username+"/"+password+"/"+headPortrait.jpg)));
portrait.setGraphic(userImage);
//And the userImage won't show up and throws Exception, because the src folder in eclipse is not refreshed!
这不是您应该使用的方法,因为它只能在开发环境中工作。部署应用程序时,编译后的代码很可能包含在JVM访问的.jar存档中 方法是将数据存储在方便的位置服务器、用户目录。。。然后从这个源加载它 示例用户目录
顺便说一句:根据用户名和密码创建目录对我来说似乎不是个好主意。密码可能包含的字符不能作为文件名的一部分,毕竟。。。此外,它还要求您在更改密码时移动文件。。。更不用说密码以明文形式存储在显眼的位置…非常感谢您的回答。至于您在BTW中提到的问题,如何为每个用户生成一个唯一且不可更改的用户ID,并使用该ID作为用户文件夹的名称。我会将用户名和密码存储在用户文件夹下的txt文件中。因此,用户名和密码可以很容易地修改。可以吗?
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" is a TextField
String password=word.getText(); //"word" is a 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 this image is included as resource
OutputStream out = Files.newOutputStream(userImage)) {
copy(in, out);
}
ImageView userImageView = new ImageView(new Image(userImage.toUri().toString()));