码迷,mamicode.com
首页 > 编程语言 > 详细

Java—FileChooser

时间:2016-12-10 19:26:14      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:tab   选中   ttext   open   this   file   fileread   文件名   close   

  //基本用法

  JFileChooser jfc = new JFileChooser();
  int result = jfc.showOpenDialog(this);

  if(result != JFileChooser.APPROVE_OPTION)

    return;  //写在button里面的

  File file = jfc.getSelectedFile();  //由于是只要选中文件,就可以获得文件路径(包含文件名),所以需要用对话框的返回值来处理正确的逻辑
  this.jtfFilePath.setText(file.getAbsolutePath());
  try
  {
    FileReader fileReader = new FileReader(file);
    BufferedReader reader = new BufferedReader(fileReader);
    String content = "";
    while((content = reader.readLine()) != null) //先赋值,后判断content的值
    {
      this.jtaContent.append(content + "\n");
    }

    reader.close();
    fileReader.close();

  }
  catch(Exception e)
  {
    e.printStackTrace();
  }

  //设置打开的默认路径:在初始化的时候可用当前工作目录

  JFileChooser jfc = new JFileChooser(System.getProperty("user.dir"));

Java—FileChooser

标签:tab   选中   ttext   open   this   file   fileread   文件名   close   

原文地址:http://www.cnblogs.com/quanxi/p/6156182.html

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