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

📄 listfiles.java

📁 一个自动查找目录文件夹下所有文件的小程序
💻 JAVA
字号:
/* * ListFiles.java * * Created on 2004年12月10日, 下午2:45 * * 我还未发现哪个常用软件中有这个功能,因此写了这个class * 你可以将光盘中的文件名列在txt文件中,便于将来的查找,或打印出来。 * 不用为了查找某个光盘中的文件而一张一张光盘去找,又慢又损耗光驱。 *  当然也可以选择某个目录,记录下目录下的所有文件名。 */package com.hunksoft;import java.awt.*;import java.awt.event.*;import java.io.*;import javax.swing.*;/** * * @author  hunk * ListFiles类能够将目录下的所有文件名写入你指定的文本文件中。 */public class ListFiles{    private static String listFileStr="";    public static void main(String[] args)    {        ListFilesFrame frame = new ListFilesFrame();        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        frame.show();    }}class ListFilesFrame extends JFrame{    public ListFilesFrame()    {        setTitle("文件清单");        setSize(400, 300);                Container con = getContentPane();        ListFilesPanel panel = new ListFilesPanel();        con.add(panel);    }}class ListFilesPanel extends JPanel{    private String openPath;    private String savePath;    private JFileChooser chooser;    private String listFileStr;    private JTextArea area;        public ListFilesPanel()    {        JButton openButton = new JButton("Open");        JButton saveButton = new JButton("Save");        area = new JTextArea(30,40);        openButton.addActionListener(new ActionListener()        {            public void actionPerformed(ActionEvent e)            {                open();            }        });        saveButton.addActionListener(new ActionListener()        {            public void actionPerformed(ActionEvent e)            {                save();            }        });        add(openButton);        add(saveButton);        add(new JScrollPane(area));    }    public void open()    {        area.setText("");        chooser = new JFileChooser();        chooser.setCurrentDirectory(new File("C:\\Documents and Settings\\Administrator\\桌面"));        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);        int result = chooser.showOpenDialog(null);        if(result == JFileChooser.APPROVE_OPTION)        {            openPath = chooser.getSelectedFile().getPath();        }        listFile(openPath);        area.setText(listFileStr);    }        public void save()    {        chooser = new JFileChooser();        chooser.setCurrentDirectory(new File("C:\\Documents and Settings\\Administrator\\桌面"));        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);        int result = chooser.showSaveDialog(null);        if(result == JFileChooser.APPROVE_OPTION)        {            try            {                savePath = chooser.getSelectedFile().getPath();                RandomAccessFile file = new RandomAccessFile(savePath,"rw");                file.write(listFileStr.getBytes());            }            catch(IOException ioe)            {                            }        }    }    public void listFile(String filepath)    {        openPath = filepath;        File file = new File(filepath);        File list[] = file.listFiles();        for(int i=0;i<list.length;i++)        {            try            {                if (list[i].isDirectory())                {                    listFile(list[i].getAbsolutePath());                                    }                else                {                    listFileStr += list[i].getAbsolutePath()+"\r\n";                }            }            catch (Exception ex)            {                listFileStr += "拒绝访问:" + list[i].getAbsolutePath()+"\r\n";            }        }    }}

⌨️ 快捷键说明

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