java导出文件时让用户选择路径怎么弄?
展开全部
使用保存文件对话框:
/**
* 保存
*/
private void saveFile(){
JFileChooser dialog = new JFileChooser();
dialog.setDialogTitle("另存为");
dialog.setFileSelectionMode(JFileChooser.FILES_ONLY);
dialog.setDialogType(JFileChooser.SAVE_DIALOG);
dialog.setFileFilter(new TextFileFilter("*.txt", "文本文档(*.txt)"));
int result = dialog.showSaveDialog(this);
if(result == JFileChooser.APPROVE_OPTION){
File file = dialog.getSelectedFile();
fileName = file.getAbsolutePath(); //得到文件全名
...
}
}
附文本类型过滤器:
import java.io.File;
import java.util.ArrayList;
import javax.swing.filechooser.FileFilter;
/**
* 设置文件打开对话框的文件过滤器
* @author developer
*/
public class TextFileFilter extends FileFilter {
private ArrayList<String> extensions = new ArrayList<String>();
private ArrayList<String> descriptions = new ArrayList<String>();
public TextFileFilter(){
super();
}
public TextFileFilter(String extension, String description) {
super();
this.extensions.add(extension);
this.descriptions.add(description);
}
@Override
public boolean accept(File pathname) {
if (pathname != null) {
if (pathname.isDirectory()) {
return true;
}
String extension = getExtension(pathname);
for(int i=0; i<extensions.size(); i++){
if(extensions.get(i).toLowerCase().endsWith(extension.toLowerCase())){
return true;
}
}
}
return false;
}
private String getExtension(File pathname) {
if (pathname != null) {
String filename = pathname.getName();
int i = filename.lastIndexOf('.');
if (i > 0 && i < filename.length() - 1) {
return filename.substring(i).toLowerCase();
}
}
return null;
}
@Override
public String getDescription() {
return descriptions.get(descriptions.size()-1);
}
}
/**
* 保存
*/
private void saveFile(){
JFileChooser dialog = new JFileChooser();
dialog.setDialogTitle("另存为");
dialog.setFileSelectionMode(JFileChooser.FILES_ONLY);
dialog.setDialogType(JFileChooser.SAVE_DIALOG);
dialog.setFileFilter(new TextFileFilter("*.txt", "文本文档(*.txt)"));
int result = dialog.showSaveDialog(this);
if(result == JFileChooser.APPROVE_OPTION){
File file = dialog.getSelectedFile();
fileName = file.getAbsolutePath(); //得到文件全名
...
}
}
附文本类型过滤器:
import java.io.File;
import java.util.ArrayList;
import javax.swing.filechooser.FileFilter;
/**
* 设置文件打开对话框的文件过滤器
* @author developer
*/
public class TextFileFilter extends FileFilter {
private ArrayList<String> extensions = new ArrayList<String>();
private ArrayList<String> descriptions = new ArrayList<String>();
public TextFileFilter(){
super();
}
public TextFileFilter(String extension, String description) {
super();
this.extensions.add(extension);
this.descriptions.add(description);
}
@Override
public boolean accept(File pathname) {
if (pathname != null) {
if (pathname.isDirectory()) {
return true;
}
String extension = getExtension(pathname);
for(int i=0; i<extensions.size(); i++){
if(extensions.get(i).toLowerCase().endsWith(extension.toLowerCase())){
return true;
}
}
}
return false;
}
private String getExtension(File pathname) {
if (pathname != null) {
String filename = pathname.getName();
int i = filename.lastIndexOf('.');
if (i > 0 && i < filename.length() - 1) {
return filename.substring(i).toLowerCase();
}
}
return null;
}
@Override
public String getDescription() {
return descriptions.get(descriptions.size()-1);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询