一个创建目录文件的程序及代码

我一般喜欢把网上的一些好文章下载下来保存在硬盘中,可是文件越来越多慢慢的自己也不知道要在那里寻找了。于是自己没事就写了一个简单的生成目录的程序,方便自己打开需要的文档。 具体代码如下:

//makeindex.java
//make file index

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

//文件过滤器
class dirFilter implements FilenameFilter
{
	String[] ext;
	public dirFilter(String[] ext)
	{
		this.ext = ext;
	}
	
	public boolean accept(File dir,String name)
	{
		if(ext==null)
			return true;
		
		name = name.toLowerCase();
		for(int i=0;i<ext.length;i++)
		{
			if(name.endsWith(ext[i])){
				return true;
			}
		}
		return false;
	}
}

//程序主类
public class makeindex extends JFrame
{
	//程序主函数
	public static void main(String[] args){
		System.setProperty("swing.plaf.metal.controlFont","宋体");
		new makeindex();
	}
	
	//类数据成员
	//调试用变量
	public final boolean DEBUG = true;
	//根容器
	private Container rootContainer = getContentPane();
	private JRootPane rootPane = getRootPane();
	
	
	private JPanel mainPanel = new JPanel();
	private JPanel buttonPanel = new JPanel();
	
	//主面板输入输出框
	private JTextField ml = new JTextField();	 		//源文件目录
	private JButton btn_ml = new JButton("目录");		//源文件目录
	private JTextField out_name = new JTextField();		//输出文件名
	private JCheckBox new_win = new JCheckBox("新窗口",true);		//是否在新网页中打开
	private JTextField title = new JTextField();		//网页标题
	private String[] lx = {"全部文件",".htm;.html",".doc;.xls",".c;.h;.java"};
	private JComboBox leixing = new JComboBox(lx);
	
	
	//按钮面板按钮数据
	private JButton make = new JButton ("生成");
	private JButton exit = new JButton ("退出");
	
	
	File files;						//一个目录对象
	String[] str_filename;			//文件名数组
	BufferedWriter html_Out;		//文件输出
	StringBuffer str_href = new StringBuffer("<a href=\"");	//一个字符串的缓冲
	
	
	//构造函数
	public makeindex()
	{
		setTitle("文件目录生成器 in Java V0.1 版");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		initFrame();
		pack();
		int w = this.getWidth()+130;
		int h = this.getHeight();
		Dimension ScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds((int)((ScreenSize.width-w)/2),(int)((ScreenSize.height-h)/2),w,h);
		setResizable(false);
		setVisible(true);
	}
	
	private void initFrame()
	{
		getMainPanel();
		getButtonPanel();
		rootPane.setDefaultButton(make);
		rootContainer.add(mainPanel,BorderLayout.NORTH);
		rootContainer.add(buttonPanel,BorderLayout.SOUTH);
	}
	
	private void getMainPanel()
	{
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		mainPanel.setLayout(gbl);
		gbc.anchor = GridBagConstraints.WEST;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.gridwidth = 1;
		gbc.gridx = GridBagConstraints.RELATIVE;
		gbc.gridy = 0;
		mainPanel.add(new JLabel("源文件目录:",JLabel.RIGHT),gbc);
		gbc.weightx = 1;
		mainPanel.add(ml,gbc);
		gbc.weightx = 0;
		mainPanel.add(btn_ml,gbc);
		gbc.gridy = 1;
		mainPanel.add(new JLabel("输出文件名:",JLabel.RIGHT),gbc);
		gbc.weightx = 1;
		mainPanel.add(out_name,gbc);
		gbc.weightx = 0;
		mainPanel.add(new_win,gbc);
		
		
		gbc.gridy = 2;
		mainPanel.add(new JLabel("网页标题:",JLabel.RIGHT),gbc);
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		mainPanel.add(title,gbc);
		
		gbc.gridy = 3;
		gbc.gridwidth = 1;
		mainPanel.add(new JLabel("文件类型:",JLabel.RIGHT),gbc);
		gbc.gridwidth = GridBagConstraints.REMAINDER;
		mainPanel.add(leixing,gbc);
		
		ml.setText(".");
		out_name.setText("index.htm");
		title.setText("文件目录生成器 in Java V0.1 版");
		leixing.setEditable(true);
		leixing.setSelectedIndex(0);
		
		ml.setToolTipText("请在这里填写源文件的目录");
		btn_ml.setToolTipText("选择源文件目录");
		out_name.setToolTipText("在源文件目录的输出文件名");
		new_win.setToolTipText("是否在新窗口中打开文件");
		title.setToolTipText("输出目录文件的标题");
		leixing.setToolTipText("输入多种类型文件之间请用分号分隔");
		

		btn_ml.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JFileChooser fc = new JFileChooser();
				fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				String mm = ml.getText();
				if(mm!=null)
				{
					fc.setCurrentDirectory(new File(mm));
				}
				if(fc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
				{
					File f = fc.getSelectedFile();
					ml.setText(f.getPath());
				}
			}
		});
		
	}
	
	
	void getButtonPanel()
	{
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		buttonPanel.add(make);
		buttonPanel.add(new JLabel("华软工作室(程旭)"));
		buttonPanel.add(exit);
		
		exit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				System.exit(0);
			}
		});
		
		make.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				makehtml();
			}
		});
	}
	
	boolean makehtml()
	{
		String str_ml = ml.getText();
		String str_out = out_name.getText();
		String str_title = title.getText();
		
		String[] exp = getLx();
		
		boolean b = new_win.isSelected();
		if((str_ml==null)||(str_out==null)){
			return false;
		}
		files = new File(str_ml);
		str_filename = files.list(new dirFilter(exp));
		
		if(DEBUG){
			for(int i=0;i<str_filename.length;i++)
				System.out.println(str_filename[i]);
		}
		
		str_out = str_ml+File.separator+str_out;
		if(DEBUG){
			System.out.println(str_out);
		}
		
		try{
			html_Out = new BufferedWriter(new FileWriter(str_out));
			
			StringBuffer ss = new StringBuffer(100);
			
			ss.append("<html>\r\n<head>\r\n<title>");
			if(str_title.equals(""))
				str_title = "文件目录生成器 in Java V0.1 版";
			ss.append(str_title);
			ss.append("</title>\r\n</head>\r\n<style type=text/css>\r\n");
			ss.append("A.a1:link {text-decoration:none;color:#00007f;}\r\n");
			ss.append("A.a1:visited {text-decoration:none;color:#00007f;}\r\n");
			ss.append("A.a1:active {text-decoration:none;color:#ff0000;}\r\n");
			ss.append("</style>\r\n<body bgcolor=#FFF3C9>\r\n");
			ss.append("<p align=center><font color=#FF00FF size=7 face=楷体_GB2312>");
			ss.append(str_title);
			ss.append("</font>\r\n<p>");
			html_Out.write(ss.toString());
			
			for(int i=0;i<str_filename.length;i++)
			{
				str_href.setLength(9);
				str_href.append(str_filename[i]);
				str_href.append("\" class=a1");
				if(b){
					str_href.append(" target=_blank>");
				}
				else{
					str_href.append(" >");
				}
				str_href.append(str_filename[i]);
				str_href.append("</a><br>");
				html_Out.write(str_href.toString());
				html_Out.newLine();
			}
			
			html_Out.write("</body>");
			html_Out.newLine();
			html_Out.write("</html>");
	
			html_Out.close();
		}
		catch(IOException ioe){
			ioe.printStackTrace();
			
			try{
				html_Out.close();
			}
			catch(IOException ioe1){
				return false;
			}
			
			return false;
		}
		
		return true;
	}
	
	String[] getLx()
	{
		String s = (String)leixing.getSelectedItem();
		if((s==null) || (s.indexOf("全部文件") != -1))
			return null;
		
		ArrayList ss = new ArrayList(10);
		int off1 = -1;
		int off2 = 0;
		
		while((off1 = s.indexOf(';',off2)) != -1)
		{
			ss.add(s.substring(off2,off1));
			off2 = off1+1;
		}
		ss.add(s.substring(off2));
		String[] exp = (String[])ss.toArray(new String[0]);
		return exp;
	}
}

<淘宝热门商品:
 

¥:15.50 

暖手宝,热水袋,手套,帽子批发零售.自封袋自粘袋不干胶袋

 

199.00 元  

JACK JONES专柜正品白色内绒毛设计羊毛毛


来源:程序员网

小小豆叮

0 Responses to "一个创建目录文件的程序及代码"

发表评论