import java.io.File;
import java.io.FileWriter;
import javax.swing.JFileChooser;
public class TestSaveAS{
public void openDic(){
String[] saveType = {"txt","java"};
JFileChooser fd = new JFileChooser(); //实例化选择器
//fd.setCurrentDirectory(new File("e:/"));//设置默认目录 打开直接默认E盘
//是否可多选
fd.setMultiSelectionEnabled(true);
//选择模式,可选择文件和文件夹
//fd.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
// fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
//fd.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); //这里去掉 否则默认文件名无法使用
//fd.setDialogTitle("保存文件"); //自定义选择框标题
fd.setSelectedFile(new File("test.java")); //设置默认文件名
//设置是否显示隐藏文件
fd.setFileHidingEnabled(true);
fd.setAcceptAllFileFilterUsed(false);
fd.setFileFilter(new ImgFileFilter2());
//设置文件筛选器
//fd.setFileFilter(new MyFilter("java"));
//fd.setFileFilter(new MyFilter("zip"));
//如果还想限制文件类型
//
//FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
fd.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("text & java",saveType));
fd.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("word","word"));
//fd.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fd.showOpenDialog(null);
File f = fd.getSelectedFile();
System.out.println("==========="+f.getName()+"============"+f.getAbsolutePath()+"===="+f.getParent()+"==="+f.getPath());
if(f != null){}
}
public void saveFile(){
JFileChooser jf = new JFileChooser();
//jf.setFileSelectionMode(JFileChooser.SAVE_DIALOG | JFileChooser.DIRECTORIES_ONLY); //FILES_ONLY
//jf.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jf.showDialog(null,null);
jf.setSelectedFile(new File("test.java")); //设置默认文件名
File fi = jf.getSelectedFile();
System.out.println("=========="+fi.getAbsolutePath()+"============"+fi.getParent()+"========"+fi.getPath());
String f = fi.getAbsolutePath()+"\\test.txt";
System.out.println("save: "+f);
try{
FileWriter out = new FileWriter(f);
out.write("successful!!!");
out.close();
}
catch(Exception e){}
}
}
//过滤器
//java.io.FileFilter
class ImgFileFilter1 implements java.io.FileFilter{
/* (non-Javadoc)
* @see java.io.FileFilter#accept(java.io.File)
*/
public boolean accept(File file) {
String filename = file.getName().toLowerCase();
// TODO Auto-generated method stub
return filename.endsWith(".gif")
||filename.endsWith(".bmp")
||filename.endsWith(".png")
||filename.endsWith(".jpeg")
||filename.endsWith(".jpg")
||file.isDirectory();
}
}
//javax.swing.filechooser.filefilter
class ImgFileFilter2 extends javax.swing.filechooser.FileFilter{
/* (non-Javadoc)
* @see javax.swing.filechooser.FileFilter#accept(java.io.File)
*/
@Override
public boolean accept(File file) {
String filename = file.getName().toLowerCase();
// TODO Auto-generated method stub
return filename.endsWith(".gif")
||filename.endsWith(".bmp")
||filename.endsWith(".png")
||filename.endsWith(".jpeg")
||filename.endsWith(".jpg")
||file.isDirectory();
}
/* (non-Javadoc)
* @see javax.swing.filechooser.FileFilter#getDescription()
*/
@Override
public String getDescription() {
// TODO Auto-generated method stub
return "*.gif,*.bmp,*.png,*.jpeg,*.jpg";
}
}
//Test
class TestFileFilter{
public void test(){
java.io.File file = new java.io.File("F:\\");
File[] files = file.listFiles(new ImgFileFilter1());
for(File f:files){
System.out.println(f.getAbsolutePath());
}
javax.swing.JFileChooser fc = new javax.swing.JFileChooser("F:\\");
fc.setFileFilter(new ImgFileFilter2());
fc.showOpenDialog(null);
}
}