程序列出文件夹中的所有文件,用户指定的扩展名不起作用

时间:2013-01-11 19:42:20

标签: java

我正在尝试创建一个程序,要求用户指定文件扩展名。然后,它会在textarea中列出具有该扩展名的所有可用文件。我做了以下计划。但是我的编译器在endsWith()语句中显示错误,如果我在没有endsWith()的情况下运行程序,则只打印第一个文件名。

感谢任何帮助确定问题。提前谢谢。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class search extends Thread implements ActionListener
{
JFrame f;
JButton bop,bser;
JTextField tf,text;
JTextArea ta;
JLabel lab,lab1;
String str;
JScrollPane scrol;
File fl;

search()
{
    f=new JFrame("Search box");
    f.setLayout(null);
    f.setSize(820,700);

    bop=new JButton("Open");
    bop.setBounds(50,600,180,30);
    bop.addActionListener(this);
    f.add(bop);

    lab=new JLabel("Extension");
    lab.setBounds(340,570,180,30);
    f.add(lab);


    bser=new JButton("Search");
    bser.setBounds(510,600,180,30);
    bser.addActionListener(this);
    f.add(bser);    

    text=new JTextField();
    text.setBounds(280,600,180,30);
    text.addActionListener(this);
    text.setHorizontalAlignment(JTextField.CENTER);
    f.add(text);

    tf=new JTextField();
    tf.setBounds(25,50,750,40);
    tf.setFont(new Font("Lucida Console",Font.BOLD,20));
    tf.setHorizontalAlignment(JTextField.CENTER);
    f.add(tf);

    ta=new JTextArea();
    scrol=new JScrollPane(ta);
    //JScrollPane.setPreferredSize(750,450);
    scrol.setBounds(25,100,750,450);

    f.add(scrol);



    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


}

public void actionPerformed(ActionEvent ae)
{
    if(ae.getActionCommand().equals("Open"))
    {
        FileDialog fd=new FileDialog(f,"Open Box",FileDialog.LOAD);
        fd.setSize(300,300);
        fd.setVisible(true);
        str=fd.getDirectory();
        tf.setText(str);                    
    }

    if(ae.getActionCommand().equals("Search"))
    {
        fl=new File(str);
        File[] flist=fl.listFiles();

        for (int i=0;i<flist.length;i++)
        {
            String newline = System.getProperty("line.separator");
            String nm=text.getText();
            if(flist[i].endsWith(nm))
            {
                if(flist[i].isFile())
                {
                    ta.setText(flist[i].getName()+newline);
                }       
            }
        }               
    }
}   

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

}

编译器没有给出任何错误。只是当我运行程序...并在目录路径中输入时,只要我按下搜索,textarea中只显示一个文件名...即使我没有在文本字段中输入任何内容,如果条件不满足,那么同样的事情也会发生。

2 个答案:

答案 0 :(得分:3)

没有File.endsWith(),您需要获取文件名称:

flist[i].getName().endsWith(nm)

您可能还想确保它是一个文件(而不是目录)以防万一。

答案 1 :(得分:1)

你需要说file.getName()。endsWith(suffix);

此外,如果您在没有先按“打开”的情况下按“搜索”,我会注意到“str”未定义。您将在此处获得空指针异常。你可能想检查一下。

编辑**

尝试以下代码。我修改了它以便它可以工作。但是,编写导致缺陷的代码的方式存在缺陷。你会看到我确定。修复这个缺陷对你来说不应该太难。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class Test extends Thread implements ActionListener
{
  JFrame f;

  JButton bop, bser;

  JTextField tf, text;

  JTextArea ta;

  JLabel lab, lab1;

  String str;

  JScrollPane scrol;

  File fl;

  Test()
{
    f = new JFrame( "Search box" );
    f.setLayout( null );
    f.setSize( 820, 700 );

    bop = new JButton( "Open" );
    bop.setBounds( 50, 600, 180, 30 );
    bop.addActionListener( this );
    f.add( bop );

    lab = new JLabel( "Extension" );
    lab.setBounds( 340, 570, 180, 30 );
    f.add( lab );


    bser = new JButton( "Search" );
    bser.setBounds( 510, 600, 180, 30 );
    bser.addActionListener( this );
    bser.setEnabled( false );
    f.add( bser );

    text = new JTextField();
    text.setBounds( 280, 600, 180, 30 );
    text.addActionListener( this );
    text.setHorizontalAlignment( JTextField.CENTER );
    f.add( text );

    tf = new JTextField();
    tf.setBounds( 25, 50, 750, 40 );
    tf.setFont( new Font( "Lucida Console", Font.BOLD, 20 ) );
    tf.setHorizontalAlignment( JTextField.CENTER );
    f.add( tf );

    ta = new JTextArea();
    scrol = new JScrollPane( ta );
    //JScrollPane.setPreferredSize(750,450);
    scrol.setBounds( 25, 100, 750, 450 );

    f.add( scrol );

    f.setVisible( true );
    f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );


  }

  public void actionPerformed( ActionEvent ae )
  {
    if ( ae.getActionCommand().equals( "Open" ) )
    {
      FileDialog fd = new FileDialog( f, "Open Box", FileDialog.LOAD );
      fd.setSize( 300, 300 );
      fd.setVisible( true );
      str = fd.getDirectory();

      if ( str != null && !str.trim().equals( "" ) )
      {
        tf.setText( str );

        // Enable the search button
        bser.setEnabled( true );
      }
      else
      {
        bser.setEnabled( false );
      }
    }

    if ( ae.getActionCommand().equals( "Search" ) )
    {
      fl = new File( str );
      File[] flist = fl.listFiles();

      for ( int i = 0; i < flist.length; i++ )
      {
        String newline = System.getProperty( "line.separator" );
        String nm = text.getText();
        if ( flist[i].getName().toLowerCase().endsWith( nm.toLowerCase() ) )
        {
          if ( flist[i].isFile() )
          {
            ta.setText( flist[i].getName() + newline );
          }
        }
      }
    }
  }

  public static void main( String args[] )
  {
    new Test();
  }
}
相关问题