使用输入输出流批量处理文档(截取新闻的标题正文)

本文介绍了一种使用Java输入输出流批量处理HTML文档的方法,实现了新闻标题与正文的自动提取,并进行了必要的文本清洗工作。
  • 使用输入输出流批量处理文档(截取新闻的标题正文)
import java.io.*;
public class Xi {

    public static void main(String[] args)throws IOException {
                zhixing();
    }
    private static void zhixing() throws IOException{
        int a = 1;
    
        for(a = 1;a<=50;a++){  //提前批量修改了文件名,然后用循环批量处理全部文档
        FileInputStream f1 = new FileInputStream("D:\\desktop\\0703作业\\"+a+".html");
        FileOutputStream f2 = new FileOutputStream("D:\\desktop\\0703作业\\txt\\"+a+"copy.txt",true);
        File xi = new File("D:\\desktop\\0703作业\\"+a+".html");
        int txt_length = (int) xi.length();
    //    System.out.println("文件长度:"+txt_length);
    
        byte [] b = new byte[txt_length];
        f1.read(b);
    //    System.out.println(b);
        String str = new String(b,"utf-8");
    //    System.out.println(str);
    //*****************截取标题*********************
        int begin1 = str.indexOf("<title>");
        int end1 = str.indexOf("|");            
        String str_bt = str.substring(begin1,end1);   
        str_bt = str_bt.replace("<title>", "");
    //*****************截取标题*********************
        String str_huanhang = "\n\n\n\n";
    //*****************截取正文*********************
        int begin2 = str.indexOf("<p>");
        int end2 = str.indexOf("<!-- 吸顶导航结束定位标记 -->");
        String str_zw = str.substring(begin2,end2);
    //*****************截取正文*********************
    //*****************删除多余元素*********************
        str_zw = str_zw.replaceAll("^<.*\n$","");
        str_zw = str_zw.replace("<p>", "");
        str_zw = str_zw.replace("<p class=\"article-editor\">", "");
        str_zw = str_zw.replace("<p>  原标题", "");
        str_zw = str_zw.replace("</p>", "");
        str_zw = str_zw.replace("<strong>", "");
        str_zw = str_zw.replace("</strong>", "");
        str_zw = str_zw.replace("</div>", "");
        str_zw = str_zw.replace("\n\n\n", "");
        str_zw = str_zw.replace("<p align=\"justify\">  ","");
        str_zw = str_zw.replace("&nbsp;","");
        str_zw = str_zw.replace("<p class=\"text\">","");
        str_zw = str_zw.replace("&gt;","");
        str_zw = str_zw.replace("<br>","");
        
    //*****************删除多余元素*********************
        
    //*****************删除图片*********************
    
            try{
                StringBuffer shanchu = new StringBuffer(str_zw);
                int begind = str_zw.indexOf("<div");
                int endd = str_zw.indexOf(">\n");
                if(begind>0){
                    shanchu.delete(begind,endd+2);
                    str_zw=shanchu.toString();
                }
    
                }
                catch(Exception e){
                    System.out.println("");
                }
    
        
            
        
    
    //*****************删除图片*********************
        System.out.println(str_bt);
    
        System.out.println(str_zw);
        byte [] out1 = str_bt.getBytes();
        byte [] out2 = str_zw.getBytes();
        byte [] out3 = str_huanhang.getBytes();
    
        f2.write(out1);
        f2.write(out3);
        f2.write(out2);
        f2.write(out3);
        f2.close();
        f1.close();
    
        System.out.println("文件:"+a+"处理完毕\n\n");
        }
        System.out.println("全部处理完毕");
}

    
}

 

转载于:https://www.cnblogs.com/string9527/p/7168370.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值