Javafx刷新src文件夹

时间:2017-08-20 11:40:38

标签: javafx refresh

我写了一个程序,每个用户都有一个帐户,他们可以改变他们的头像。当他们登录时,将创建目录“src / username / password”,并且还将创建图像“src / username / password / headPortrait.jpg”。然后默认的headPortrait应该显示在标签上,但问题出现了:图像被添加到“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!

1 个答案:

答案 0 :(得分:1)

这不是您应该使用的方法,因为它只能在开发环境中使用。部署应用程序时,编译的代码很可能包含在JVM访问的.jar存档中。

可行的方法是将数据存储在方便的位置(服务器,用户目录,...)并从此源加载。

示例(用户目录)

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()));

BTW:从用户名和密码创建目录对我来说似乎不是一个好主意。密码可能包含毕竟不能成为文件名一部分的字符...此外,它还要求您在更改密码时移动文件...更不用说密码以明文形式存储在一个突出显示中地方...