网页文件管理,整理你散乱的下载的网页文件

网页文件管理

在网上浏览,遇到好的文章,你是选择保存网页还是复制粘贴到word再保存,或是添加到自己的博客?

如果你喜欢保存网页,那么这个小工具,一定能够帮上你的忙。

它能把各目录中的网页文件通过超链接 链接起来,方便你阅读下载的网页,

从此你再也不需要反反复复的打开/关闭浏览器了,

 

使用方法:

1. 选择自己保存网页的文件夹,

2.点击确定,开始构建

3. 同样可以 移除链接各目录和文档的文件

 

BuildFile.java

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.PrintStream;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class BuildFile extends JFrame implements ActionListener{
    private static final long serialVersionUID = 1L;
    private JPanel jp = new JPanel();;
    JButton b1,b2,b3;
    JTextField tf1;
    JFileChooser fc;
    TextArea area;
    PrintStream ps;
    //Graphics g = this.getGraphics();
    public BuildFile(){
        this.initial();
    }
    public void initial() {
        this.setTitle("网页文件管理");
        this.setSize(690, 490);
        this.setResizable(false);
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(this);
        Container c = this.getContentPane();
        c.setLayout(null);
        jp.setBounds(0, 0, this.getWidth(), this.getHeight());
        jp.setLayout(new FlowLayout());
        jp.setBackground(Color.getHSBColor(106, 99, 199));
        tf1 = new JTextField(25);
        
        area = new TextArea("",25,85);
        b1 = new JButton("选择目录…");b1.setAutoscrolls(false);
        b2 = new JButton("确定");
        b3 = new JButton("移除构建的文件");
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        jp.add(tf1);
        jp.add(b1);
        jp.add(b2);
        jp.add(b3);
        jp.add(area);
        c.add(jp);
        
        
        this.setVisible(true);
    }
    //构建目录网页文件,把各目录中的网页文件通过超链接 链接起来
    public void build(File file,TextArea area){
        if (!file.toString().contains("files")) {
            File[] fileList = file.listFiles();
            String path = file.getPath();
            try {
                String[] strs = path.split("");
                ps = new PrintStream(path + "//" + strs[strs.length - 1]
                        + ".htm");
                ps
                        .print("<!DOCTYPE html PUBLIC /"-//W3C//DTD XHTML 1.0 Transitional//EN/" /"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd/"><HTML xmlns=/"http://www.w3.org/1999/xhtml/"><HEAD><META http-equiv=/"Content-Type/" content=/"text/html; charset=GBK/"><html> <head><title>"
                                + strs[strs.length - 1]
                                + "</title>" +
                                        "<style type='text/css'>"
                                        +"<!--"
                                        +" text-align:center;margin:0px;overflow:auto;font-size:14px;color:#003; text-decoration:none;"
                                        
                                        +" --></style>" +
                                        "" +
                                        "" +
                                        "" +
                                        "</head> <body><br/><br/><table align='center' border='0' width='1000px'>");
            } catch (Exception e) {

            }
            for (int i = 0; i < fileList.length; i++) {
                if (!fileList[i].toString().contains("files")) {
                    if (fileList[i].isDirectory()) {
                        String[] strs = fileList[i].toString().split("");
                        ps.append("<a href=/"file:///" + fileList[i] + "//"
                                + strs[strs.length - 1] + ".htm/">"
                                + fileList[i] + "</a><br>");
                        String str1 = "Building Directory: " + fileList[i].getPath()+fileList[i].getName()
                                + "/n";
                        area.append(str1);
                        area.update(area.getGraphics());
                        // area.append("Building Directory:
                        // "+fileList[i]+"……/n");
                    }
                    if (fileList[i].toString().contains("htm")
                            || fileList[i].toString().contains("html")) {
                        ps.append("<a href=/"file:///" + fileList[i] + "/">"
                                + fileList[i] + "</a><br>");
                        String str1 = "Building Directory: " + fileList[i].getPath()+fileList[i].getName()
                                + "/n";
                        area.append(str1);
                        area.update(area.getGraphics());
                    }
                }
            }

            ps.append("</table></body></html>");
            for (int j = 0; j < fileList.length; j++) {
                if (fileList[j].isDirectory()) {
                    build(fileList[j], area);
                }
            }
        }
    }
    
    public void delete(File file,TextArea area){
        if (!file.toString().contains("files")) {
            File[] fileList = file.listFiles();
            //for(int t=0; t<fileList.length; t++)
                //System.out.println(fileList[t]);
            // System.out.println(file.toString().codePointCount(file.toString().length()-6,
            // file.toString().length()-1));
            String path = file.getPath();
            try {
                String[] strs = path.split("");
                String str = strs[strs.length-1];
                
                str = str+".htm";
                for(int i=0; i<fileList.length; i++){
                    if(str.equals(fileList[i].getName().toString())){
                        System.out.println(fileList[i]);
                        fileList[i].delete();
                    }
                }
            } catch (Exception e) {

            }
        
            for (int j = 0; j < fileList.length; j++) {
                if (fileList[j].isDirectory()) {
                    delete(fileList[j], area);
                }
            }
        }
    }
    

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

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == b1){
            fc = new JFileChooser();
            fc.setDialogTitle("请选择目录…");
            fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
            
            fc.setSelectedFile(new File(tf1.getText()));
            
            fc.showOpenDialog(this);
            File file = fc.getCurrentDirectory();
            String str = file.toString();
            
            tf1.setText(str);
            fc.updateUI();
        }
        else if(e.getSource() == b2){
            if(null != b2){
                String str = tf1.getText();
                //System.out.println(str);
                //str = str.replaceAll("", "//");
                
                System.out.println(str);
                File file = new File(str);
                this.build(file,area);
                
            }
        }
        else if(e.getSource() == b3){
            if(null != b3){
                String str = tf1.getText();
                System.out.println(str);
                //str = str.replaceAll("", "//");
                System.out.println(str);
                File file = new File(str);
                this.delete(file,area);
            }
        }
        
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值