我在java中开发了简单的记事本应用程序。当我想保存文件时,我想在该文件中添加.txt扩展名。这在文件对话框中工作正常
String filename;
FileDialog fd = new FileDialog(Editor.this,"Save File",FileDialog.SAVE);
fd.show();
if (fd.getFile()!=null)
{
filename = fd.getDirectory() + fd.getFile()+".txt";
setTitle(filename);
}
在FileDialog中使用finename.txt
工作正常但是当我接受
String filename;
JFileChooser save = new JFilechooser();
尝试添加扩展程序
filename =save.getDirectory() + save.getFile()+".txt";
这不起作用我怎样才能解决这个问题?
答案 0 :(得分:2)
在JFileChooser
中获取您使用的文件:
File currentFile= save.getCurrentFile();
然后才能得到你需要的东西(例如路径):
String fileName = file.getPath();
在旁注中,JFileChooser
有一个很好的文档,请务必阅读:http://docs.oracle.com/javase/6/docs/api/javax/swing/JFileChooser.html