使用读写文件读取网页内容

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class Task1 {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		
		selectString("D:\\7旬老人流落街头.txt","<p>  临空","老人。","<title>7旬","新闻</title>","D:\\7旬老人流落街头 - 副本.txt");
		selectString("D:\\保安.txt","<p>  去年","城晚报","<title>保安","新闻</title>","D:\\保安 - 副本.txt");
		selectString("D:\\村中直径1米多古柏被盗伐.txt","<p>  中新","此事。","<title>村中","新闻</title>","D:\\村中直径1米多古柏被盗伐 - 副本.txt");
		selectString("D:\\老太被撞拒收司机钱.txt","<p>  <strong>老人","名单。","<title>老太","新闻</title>","D:\\老太被撞拒收司机钱 - 副本.txt");
		selectString("D:\\邻居家闯入凶徒.txt","<p>  不到","帮忙。","<title>邻居","新闻</title>","D:\\邻居家闯入凶徒 - 副本.txt");

		
	}

	private static void selectString(String FileName,String str1,String str2,String strTitle1,String strTitle2,String CopyFileName) throws FileNotFoundException,
			IOException, UnsupportedEncodingException {
		//构造对象
//		FileName = "D:\\学习习总讲话.txt";
		
		
		FileInputStream fis1 = new FileInputStream(FileName);
		File f1 = new File(FileName);
		
		//读取文件
		int f1len = (int) f1.length();
		byte[] b = new byte[f1len];
		
		fis1.read(b);
		
		String str = new String(b,"UTF-8");
		
		//System.out.println(str);
		
		//关闭
		fis1.close();
		
//		str1 = "<p>央视";
//		str2 = "信心。" ;
//		
//		strTitle1 = "<title>香港";
//		strTitle2 = "新闻</title>";
		
		int index1 = str.indexOf(str1);
		int index2 = str.indexOf(str2);
		
		int indexTitle1 = str.indexOf(strTitle1);
		int indexTitle2 = str.indexOf(strTitle2);
		
		String strTitle = str.substring(indexTitle1+7,indexTitle2+2);
		String str3 = str.substring(index1,index2+3);
		String str4 = str3.replace("<p>", "");
		String str5 = str4.replace("</p>", "");
		
		System.out.println(strTitle);
		System.out.println(str5);
//		String str6 ="";
//		String str4 = str.substring(1, index1-1);
//		String str7 = str;
//		str7.replace(str4, str6);
//		String str5 = str.substring(index2+1,str.length());
//		str.replace(str5, str6);
//		
//		System.out.println(str);
		
		//构造对象
//		CopyFileName = "D:\\学习习总讲话 - 副本.txt";
		FileOutputStream fos1 = new FileOutputStream(CopyFileName);
		
		//写入
		byte[] b1 = strTitle.getBytes();
		byte[] b2 = str5.getBytes();
		
		fos1.write(b1);
		
		String str6 =System.getProperty("line.separator");
		byte[] b3 = str6.getBytes();
		
		fos1.write(b3);
		
		int len = 0;		
		
//		System.out.println(b2.length);
		while(len<b2.length){
			
			fos1.write(b2[len]);			
			len++;
			
		}		
		
		//关闭		
		fos1.close();
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值