Java GetFile returns incorrect filename after using SetFile

♀尐吖头ヾ 提交于 2019-12-12 16:09:50

问题


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

The problem is when I use setFile() to set the filename to

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

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.


回答1:


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.

Edit:
Fixed in:

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



回答2:


This is bug JDK-8021943: FileDialog getFile returns corrupted string after previous setFile.

Affected JVM versions: 7u5,7u15,7u21,7u25,7u40

Fixed for Java 8




回答3:


Function getSaveFilePath is fine and work as expected for me. The problem is probably in how do you use it. Could you please post piece of code that calls this function?

Here is how I called it:

public class FileDialogTest
{
    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 ();
    }

    public static void main (String [] args) throws Exception
    {
        String title = "NBA - In the Zone P.L. (Video by JESSExAKAxViCiOUS)";
        title = getSaveFilePath (title);
        System.out.println (title);
    }
}


来源:https://stackoverflow.com/questions/14972664/java-getfile-returns-incorrect-filename-after-using-setfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!