java的swing设置弹出保存文件窗口

package com.demo.test;

import java.io.File;
import java.io.FileOutputStream;

import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Test {

public static void fileChooser() {

  Locale.setDefault(Locale.ENGLISH);//设置语言

  //设置皮肤,搜索关键字beautyeye下载相关的jar包
  try{
      org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
    } catch(Exception e){
  }

  JFileChooser chooser = new JFileChooser();
  FileNameExtensionFilter filter = new FileNameExtensionFilter("csv");
  // 设置文件类型
  chooser.setFileFilter(filter);

  chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//设置保存路径
  //chooser.showOpenDialog(null);//设置是否先点击打开在保存

  // 打开选择器面板
  int returnVal = chooser.showSaveDialog(new JPanel());
  // 保存文件从这里入手,输出的是文件名
  if (returnVal == JFileChooser.APPROVE_OPTION) {
    System.out.println("你打开的文件夹是: " + chooser.getSelectedFile().getPath());
    String path = chooser.getSelectedFile().getPath();
    System.out.println("path:"+path);
  try {
      File f = new File(path + "\\" + ".csv");
      System.out.println(f.getAbsolutePath());
      f.createNewFile();
      FileOutputStream out = new FileOutputStream(f);

      out.write("测试样例".getBytes());
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

public static void main(String[] args) {
    fileChooser();
  }
}

posted @ 2016-11-19 20:11  彬灵晨夏  阅读(2342)  评论(0编辑  收藏  举报