读HTML文件并计算“新浪”出现的次数

读HTML文件并计算“新浪”出现的次数

读法一

package test2;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author 啦啦啦
 *
 */
public class T7 {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) {
		
		// 自带缓冲,不用数组
		FileReader reader = null;
		BufferedReader br = null;
		int count1 = 0;
		int count2 = 0;
		try {
			reader = new FileReader("D:\\大二上资料\\java高编\\实验\\test.html");
			// 被传递的流叫 节点流
			// 外部负责包装的流叫 包装流/处理流
			br = new BufferedReader(reader);

			// 读一行
			// 不带换行符
			String firstLine = br.readLine();
			System.out.println(firstLine);

			// 给一个默认文本行为null
			// 该方法读到最后返回null
			String s = null;
			while ((s = br.readLine()) != null) {
//				String s1 = "新浪";
				String s2 = s.replace("新浪", "夔"); // 将字符串中两个字替换为一个字,创建新的字符串
				int num = s2.length();// 两者之差为出现次数
				count1 += num;
				count2 += s.length();
				// 读一行,但不带换行符
				System.out.println(s);
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		System.out.println("新浪出现的次数为" + (count2 - count1));

		// 关闭里面的,外面会自动关闭
		try {
			if (br != null) {
				br.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

读法二

/**
 * 
 */
package test2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @author 啦啦啦
 *
 */
public class T1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		FileInputStream fis = null;
		
		try {
			
			fis = new FileInputStream("D:\\大二上资料\\java高编\\实验\\test.html");
			String word = "新浪";
			//available
			byte[] bytes = new byte[fis.available()];
			
			int readCount = fis.read(bytes);
			String s = new String(bytes);
			System.out.println(s);
			String s1=s.replace("新浪","穡"); //将字符串中两个字替换为一个字,创建新的字符串
			System.out.println("出现的次数为:"+(s.length()-s1.length()));//两者之差为i出现次数
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(fis != null){
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}

另外几种读取HTML文件的方法

1

package test2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * 
 */

/**
 * @author 啦啦啦
 *
 */
public class T2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		
		//缺点:
		//一次只读取一个字节(byte)
		//这样内存和硬盘交互太频繁,基本上事件/资源都耗费在交互上了
		//没有做到一次读取多个字节
		
		FileInputStream fis = null;

		try {
			fis = new FileInputStream("D:\\大二上资料\\java高编\\实验\\test.html");
			
			//采用byte数组,一次读取多个字节,最多读取数组.length个字节
			//准备一个长度为4的byte数组,一次最多读取4个字节
			byte[] bytes = new byte[4];
			
			
			//简化
			int readCount = 0;    //默认值
			//read()方法,后面没有数据可读时,会返回-1
			//使readCount等于read()方法的返回值,若不等于-1,就说明有数据可读
			while ((readCount = fis.read(bytes)) != -1) {
//				读到就输出(读到多少就把多少转换成字符串)
				System.out.print(new String(bytes, 0, readCount));
			}
			//啥都能读
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

}

2

package test2;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * 
 */

/**
 * @author 啦啦啦
 *
 */
public class T3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		FileReader reader = null;
		try {
			//创建文件输入流
			reader = new FileReader("D:\\大二上资料\\java高编\\实验\\test.html");
			//准备一个char数组
			char[] chars = new char[4];//一次读取4个字符
			int readCount = 0;
			//按字符的方式读取
			while((readCount = reader.read(chars)) != -1){
				String s = new String(chars,0,readCount);
				System.out.print(s);
				
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(reader != null){
				try {
					reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}

3

/**
 * 
 */
package test2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author 啦啦啦
 *
 */
public class T4 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		File file = new File("D:\\大二上资料\\java高编\\实验\\test.html");
		FileReader fr = null;
		try {
			
			fr = new FileReader(file);
			//read()返回读到的字符,否则返回-1
			int data = 0;
			while((data = fr.read()) != -1){
				System.out.print((char)data);
				data = fr.read();
			}
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(fr != null){
				try {
					fr.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值