📄 filelister.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.DateFormat;
import java.util.Date;
public class FileLister extends Frame implements ActionListener,ItemListener
{
private List list;//用于显示文件列表
private TextField details;//用于显示选中的文件(夹)详细情况
private Panel buttons;//用于放置两个button
private Button up,close;
private File currentDir;
private FilenameFilter filter;//文件筛选器
private String[] files;//放置文件(夹)名的数组
private DateFormat dateFormatter=DateFormat.getDateTimeInstance(DateFormat.SHORT,DateFormat.SHORT);
//日期格式化,注意getDateTimeInstance有两个参数,分别表示日期和时间
/*
*说明:
*DateFormat shortDateFormat =DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
*DateFormat mediumDateFormat =DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
*DateFormat longDateFormat =DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
*DateFormat fullDateFormat =DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
*对应的日期时间格式分别如下:
*9/29/01 8:44 PM
*Sep 29, 2001 8:44:45 PM
*September 29, 2001 8:44:45 PM EDT
*Saturday, September 29, 2001 8:44:45 PM EDT
**/
public FileLister(String directory,FilenameFilter filter)
{
super("File Lister");//窗口标题
this.filter=filter;//文件筛选器
addWindowListener(new WindowAdapter()
{//关闭窗口
public void windowClosing(WindowEvent e)
{
dispose();
}
});
list=new List(12,false);//一个12行的list表单
list.setFont(new Font("MonoSpaced",Font.PLAIN,14));//设置字体
list.addActionListener(this);//捕捉双击鼠标的行为
list.addItemListener(this);//捕捉单击鼠标的行为(选中项)
details=new TextField();//显示文件详情
details.setFont(new Font("MonoSpaced",Font.PLAIN,12));
details.setEditable(false);//readonly
buttons=new Panel();
buttons.setLayout(new FlowLayout(FlowLayout.RIGHT,15,15));//靠右排列项,间距15 15
buttons.setFont(new Font("SansSerif",Font.BOLD,14));
up=new Button("上一级目录");
close=new Button("关闭");
up.addActionListener(this);
close.addActionListener(this);
buttons.add(up);
buttons.add(close);
this.add(list,"Center");
this.add(details,"North");
this.add(buttons,"South");
this.setSize(500,300);
listDirectory(directory);//列出指定目录
}
public void listDirectory(String directory)
{//此方法作用是列出文件,并不涉及文件的操作
File dir=new File(directory);//创建文件(目录)对象
if(!dir.isDirectory())
{
throw new IllegalArgumentException("FileLister:no such directory");
}
files=dir.list(filter);//列出文件并用filter筛选
java.util.Arrays.sort(files);//排序文件
list.removeAll();//清除此前列出的文件列表
list.add("上一级目录");
for(int i=0;i<files.length;i++)
list.add(files[i]);//将文件加入到列表框
this.setTitle(directory);
details.setText(directory);
currentDir=dir;
}
public void itemStateChanged(ItemEvent e)
{ //选中项(单击)的相应行为,并不涉及双击项目
int i=list.getSelectedIndex()-1;
//因为我们在列表中的第一项是“上一级目录”,因此列表的selectedindex要比files的index大一,
//而我们要获取files的index,就要相应减去1
if(i<0)
return;
String filename=files[i];
File f=new File(currentDir,filename);
if(!f.exists())
throw new IllegalArgumentException("FileLister:no such file or directory");
String info=filename;
if(f.isDirectory())
info+=File.separator;//如果是目录就在后面增加一个分隔符,windows下是“\”,Linux下是“/”
info+=" "+f.length()+ " bytes ";
info +=dateFormatter.format(new java.util.Date(f.lastModified()));
if(f.canRead()) info+=" Read";
if(f.canWrite()) info+=" Write";
details.setText(info);
}
public void actionPerformed(ActionEvent e)
{//双击项目、单击button的行为定义,由此我们也发现,对于列表的双击和对按钮的单击都属于ActionEvent
if(e.getSource()==close) this.dispose();//按钮close的行为定义
else if(e.getSource()==up)
{
up();
}//按钮up的行为定义
else if(e.getSource()==list)
{
int i=list.getSelectedIndex();
if(i==0) up();//如果selectedindex为0,即第一项,就是“上一级目录”那一项,就调用up()方法
else
{
String name=files[i-1];
File f=new File(currentDir,name);
String fullname=f.getAbsolutePath();//取得文件(目录)的绝对路径
if(f.isDirectory())
listDirectory(fullname);//如果是目录,则列出
else new FileViewer(fullname).setVisible(true);
//否则创建一个FileViewer类,即要弹出我们昨天所写的FileViewer窗口来读取文件的内容
}
}
}
protected void up()
{//列出父目录
String parent=currentDir.getParent();
if(parent==null)
return;
listDirectory(parent);//一个递归
}
public static void usage()
{
System.out.println("Usage: java FileLister [directory_name] "+"[-e file_extension]");
System.exit(0);
}
public static void main(String args[]) throws IOException
{
FileLister f;
FilenameFilter filter=null;
String directory=null;
for(int i=0;i<args.length;i++)
{
if(args[i].equals("-e"))
{
if(++i>args.length)
usage();
final String suffix=args[i];//文件扩展名(-e后接着的输入参数,因为前面已经有++i)
filter=new FilenameFilter()
{//筛选器filter要实现FilenameFliter接口
public boolean accept(File dir,String name)
{
/*Tests if a specified file should be included in a file list.
* dir - the directory in which the file was found.
* name - the name of the file.
* 就是说,如果文件的扩展名跟我们输入的一致,则返回true
* 否则判断文件是否为一个目录,如果是一个目录,那么也返回
* 而其他不符合我们输入的文件,则不列出
*/
if(name.endsWith(suffix))
return true;
else
return (new File(dir,name)).isDirectory();
}
};
}
else
{
if(directory!=null)
usage();//我们初始化的目录是null的,如果非null,表明程序有问题,退出
else
directory=args[i];//否则将-e前一个参数赋给directory
}
}
if(directory==null)
directory=System.getProperty("user.dir");//如果没有输入目录参数,那么就用用户当前目录
f=new FileLister(directory,filter);//创建一个FileLister,这里有两个参数,一个是目录,一个是筛选器,这个筛选器就是我们刚才创建的那个,注意这个类中我们并没有在main方法之外单独创建一个筛选器方法
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
f.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -