码迷,mamicode.com
首页 > Windows程序 > 详细

swing

时间:2016-05-05 22:23:15      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

//通过swing控件读取文件

private static void chooseFile() {
  JFileChooser jfc = new JFileChooser();// 初始化文件选择器
  FileNameExtensionFilter filter = new FileNameExtensionFilter("TXT","txt");// 设置文件过滤,TXT为提示用户选择文件类型,txt为文件过滤后缀
  jfc.setFileFilter(filter);// 将文件过滤加载到选择器中
  int returnVal = jfc.showOpenDialog(null);
  if (returnVal == JFileChooser.APPROVE_OPTION) {
  // 获得打开的文件
  File file = jfc.getSelectedFile();
  File out = new File(file.getParent() + "\\jiagkun.txt");
  try {
    InputStreamReader in = new InputStreamReader(
    new FileInputStream(file), "UTF-8");
    BufferedReader buff = new BufferedReader(in);
    BufferedWriter write = new BufferedWriter(new FileWriter(out));
    String text = null;
    while ((text = buff.readLine()) != null) {
      write.write(text + "\n");
    }
    buff.close();
    write.close();
  } catch (FileNotFoundException e1) {
  } catch (UnsupportedEncodingException e1) {
  } catch (IOException e1) {
  }
  }

swing

标签:

原文地址:http://www.cnblogs.com/lansetuerqi/p/5463433.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!