⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filelister.java

📁 文件目录显示,并能输出txt本文文件(显示出来各文件夹下所有文件名称等)
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.DateFormat;
import java.util.Date;

import javax.swing.filechooser.FileView;

public class FileLister extends Frame implements ActionListener, ItemListener {

	/**
	 * @param args
	 */

	private List list;

	private TextField details;

	private Panel buttons;

	private Button up, close;

	private File currentDir;

	private FilenameFilter filter;

	private String[] files;

	private DateFormat dateFormatter = DateFormat.getDateTimeInstance(
			DateFormat.SHORT, DateFormat.SHORT);

	public FileLister(String directory, FilenameFilter filer) {
		super("File Lister");
		this.filter = filter;
		try {
			System.setOut(new PrintStream("e:/a.txt"));
		} catch (FileNotFoundException e1) {
			// TODO 自动生成 catch 块
			e1.printStackTrace();
		}

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose();
			}
		});

		list = new List(12, false);
		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);

		buttons = new Panel();
		buttons.setLayout(new FlowLayout(FlowLayout.RIGHT, 15, 5));
		buttons.setFont(new Font("SansSerif", Font.BOLD, 14));

		up = new Button("UP a Directory");
		close = new Button("Close");
		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, 350);

		listDirectory(directory);
		System.out.println(directory);
	}

	private void listDirectory(String directory) {
		// TODO 自动生成方法存根
		File dir = new File(directory);
		if (!dir.isDirectory())
			throw new IllegalArgumentException("FileLister:no such directory");

		files = dir.list(filter);
		java.util.Arrays.sort(files);
		list.removeAll();
		list.add("[Up to Parent Directory]");
		for (int i = 0; i < files.length; i++){
			list.add(files[i]);
		System.out.println(files[i]);
		}
		this.setTitle(directory);
		details.setText(directory);
		
		currentDir = dir;
	}

	public static void main(String[] args) throws IOException{
		// TODO 自动生成方法存根
		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 suffis = args[i];
				filter = new FilenameFilter(){

					public boolean accept(File dir, String name) {
						// TODO 自动生成方法存根
						if(name.endsWith(suffis)) return true;
						
					else return (new File(dir,name)).isDirectory();
				 }
				};
			}
			else{
				if(directory!=null) usage();
				else directory = args[i];
			}
		}
		if(directory==null) directory=System.getProperty("user.dir");
		f = new FileLister(directory,filter);
		f.addWindowListener(new WindowAdapter(){
			public void windowClosed(WindowEvent e){
				System.exit(0);
			}
		});
		f.show();
	}

	public void itemStateChanged(ItemEvent e) {
		// TODO 自动生成方法存根
		int i = list.getSelectedIndex() - 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;
		info += "" + f.length() + "byte";
		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) {
		// TODO 自动生成方法存根
		if (e.getSource() == close)
			this.dispose();
		else if (e.getSource() == up) {
			up();
		} else if (e.getSource() == list) {
			int i = list.getSelectedIndex();
			if (i == 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).show();
				}
			}
		}
	}

	private void up() {
		// TODO 自动生成方法存根
		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.out.println(0);
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -