Java的使用的GetFile后SetFile返回不正确的文件名 [英] Java GetFile returns incorrect filename after using SetFile

查看:222
本文介绍了Java的使用的GetFile后SetFile返回不正确的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些Java code:

I have some Java code:

public static String getSaveFilePath(String title2)
  {
  FileDialog fd = new FileDialog(new Frame(), "Save As...", 1);
    fd.setFilenameFilter(new FilenameFilter() {
      public boolean accept(File dir, String name) {
        return name.endsWith(".mp3");
      }
    });
    fd.setFile(title2 + ".mp3");
    fd.setVisible(true);
    String str3 = fd.getFile();
    String str4 = fd.getDirectory();
    if (str4 == null) return null;
    str3 = str3.replace(".mp3", "");
    str3 = str3 + ".mp3";
    String str5 = str3;
    File localFile = new File(str4, str5);
    return localFile.getPath();
}

问题是,当我使用 setFile()来的文件名设置为

NBA - In the Zone P.L. (Video by JESSExAKAxViCiOUS)

中所示的对话框之前,则显示在对话框的时候,我将文件另存为 abc.mp3
我用的GetFile()再次,我得到 SExAKAxViCiOUS).MP3 - 在那里,因为它应该返回的名称我的文件选择保存到 abc.mp3

before the dialog is shown, and when the dialog is shown, I save the file as abc.mp3 and I use getFile() again, I get SExAKAxViCiOUS).mp3 - where as it should return the name of the file I selected to save to, abc.mp3.

谁能告诉我什么,我做错了什么?此外,如果你有一个更好的跨平台解决方案,以显示保存文件对话框,请与我分享。

Can someone tell me what I am doing wrong here? Also if you have a better cross platform solution to show a save file dialog please share it with me.

推荐答案

我使用Java 1.7.0_21时有同样的问题。看来,只要你选择一个文件名或路径比你previously设定setFile(一短),那么新的文件将被getDirectory()和的GetFile(返回目录(!))返回休息的previous文件名。
当使用Java 1.6,它正常工作,所以我想这是1.7的bug。

I have the same issue when using Java 1.7.0_21. It seems that whenever you choose a filename or path that is shorter than the one you previously set with setFile(), then the new file will be returned as directory(!) by getDirectory() and getFile() returns the rest of the previous filename. When using Java 1.6, it works as expected, so I guess this is a bug in 1.7.

编辑:结果
修正:


Fixed in:

  • Java 7u60b01
    (JDK-8024349 : FileDialog getFile returns corrupted string after previous setFile)
    --> Early Access Release downloads
  • Java 8 (JDK-8021943)

这篇关于Java的使用的GetFile后SetFile返回不正确的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆