JFileChooser fc = new JFileChooser("C:/");
fc.setMultiSelectionEnabled(true);
if(fc.showOpenDialog(null)!= JFileChooser.APPROVE_OPTION) {
File[] files = fc.getSelectedFiles();
System.out.print("size="+files.length);
// :-) :-) :-)
}

解决方案 »

  1.   

    新建一文本文件会有点问题,可以选定目录,然后另外用个文本框指定文件。名。    JFileChooser chooser = new JFileChooser( this.strSelectedPreAddr );
          chooser.setMultiSelectionEnabled(false);
          chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
          File file;
          String addr = "";
          if( chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION ) {
            file = chooser.getSelectedFile();
      

  2.   

    JFileChooser fc = new JFileChooser("C:/");
    fc.setMultiSelectionEnabled(true);
    System.out.print("hello0");
    if(fc.showOpenDialog(null)!= JFileChooser.APPROVE_OPTION) {
    System.out.print("hello1");
    File[] files = fc.getSelectedFiles();
    System.out.print("hello2");
    System.out.print("size="+files.length);
    // :-) :-) :-)
    }hello1,hello2都打印不出来。
      

  3.   

    bluesmile979(笑着) 
    this.strSelectedPreAddr
    是什么类型的变量?初始值是什么?
      

  4.   

    int returnVal = jFileChooser1.showDialog(this,"Select File");
        String filename = new String();
        if(returnVal == JFileChooser.APPROVE_OPTION)
        {
          try
          {
            filename = jFileChooser1.getSelectedFile().getAbsolutePath();
            this.setTitle("OPEN"+filename);
            jEditorPane1.setText(this.readfile(filename));
          }catch(Exception ie){}
        }
      

  5.   

    String类型的,设定JFileChoose的默认路径。考给你的是一小段代码,没有修改:)在这里我前面会给他一个初始值this.strSelectedPreAddr = System.getProperty( "user.dir" );意思是设定程序所在目录的物理盘符根目录为JFileChoose的默认路径比如我的程序在C:\hehe\haha.java;得到的就是C:\this.strSelectedPreAddr
    ~~~~~~~~
    这里this就是说是我这个类的一个属性,str说明他是字符串,我的习惯,呵呵。 
      

  6.   

    bluesmile979
    兄台的习惯很好啊。
    不过,你给的代码和helpall() 的都是不能返回路径文件名。 heyday(水中天) 这位兄台的就可以。不知道为什么。
      

  7.   

    addr = file.getAbsolutePath();
      

  8.   

    导出时,你可以选定到目录
    设定选择模式chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);就是只能选目录。另外提供文本域来让用户输入文件名,我在最上面就说过了。新建只能这么做,如果已有就和导出一样了。
      

  9.   

    helpall() ,我们都没有把getAbsoultPath()贴出来。我们的代码没有问题的。
      

  10.   

    二位兄台,是我的疏忽,你们给我的代码都是可以用的。SORRY!
    导出我还是没弄出来。
    现在的问题是:我输入一个文件名,不知道如何在该目录下一输入的名字为文件名来新建这个文本文件。如果输入的文件在该目录下已经存在,我想把它覆盖掉。bluesmile979(笑着) 老兄,帮帮忙吧。
      

  11.   

    目录+文件名你就得到AbsoultPath了。下面的函数基本处理就是读一个文件然后写到另一个文件,其中包括你的要求,现在有事要做,代码直接贴给你,你看看,有问题再说:)。   public void HandleFileStream() {
            String readline = "";
            try{
                RandomAccessFile openfile = new RandomAccessFile( this.OpenPathFileName,"r" );
                File file = new File( this.SavePathFileName + "temp" );
                System.out.println("保存アドレス" + this.SavePathFileName );
                RandomAccessFile savefile = new RandomAccessFile( file,"rw" );            readline = openfile.readLine();
                while( true ) {
                    if( readline != null ) {
                        System.out.println( readline );
                        if( readline != null ) {
                            savefile.writeBytes( this.StringHandle( readline ) );
                            savefile.writeBytes("\n");
                        }
                        readline = openfile.readLine();
                    } else {
                        savefile.close();
                        openfile.close();
                    break;
                    }
                }
                File filesource = new File( this.SavePathFileName );
                if( filesource.exists() == true ) {
                    filesource.delete();
                }
                file.renameTo( new File( this.SavePathFileName ) );
            }catch( FileNotFoundException ee ) {
                System.out.println(ee.getMessage());
            }catch( IOException e ) {
                System.out.println(e.getMessage());
            }
        }
      

  12.   

    兄台,我的意思是这样的。
    我想用JFileChooser选择一个目录,然后输入一个文件名。
    根据这个文件名在该目录下建一个新的空的文本文件。然后在从数据库里面取出数据按照一定的格式写到这里面去。
    String strSelectedPreAddr = System.getProperty( "user.dir" );
    JFileChooser chooser = new JFileChooser( strSelectedPreAddr);
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
          File file;
      if( chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION ) {
            file = chooser.getSelectedFile();
    String strTemp=file.toString();
    System.out.println("****filename****"+file.toString());
    File fl=new File(strTemp);
    }
    运行后可以得到"****filename****"d:\jdbc\test.txt
    但是这样File fl=new File(strTemp);并不能在jdbc该目录下建了test.txt文件。我写的明白么?
      

  13.   

    if( !file.exists() ){
                file.createNewFile();
            }
      

  14.   

    bluesmile979(笑着) 
    谢谢,这正是我想得到的。
    你真是高手。以后有什么不会的还的请教你啊。
    我的QQ是4951224。请加我。