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.
解决方案
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: