java getfiles,Java的使用的GetFile后SetFile返回不正确的文件名

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值