读法一
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;
public class T7 {
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);
String s = null;
while ((s = br.readLine()) != null) {
String s2 = s.replace("新浪", "夔");
int num = s2.length();
count1 += num;
count2 += s.length();
System.out.println(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("新浪出现的次数为" + (count2 - count1));
try {
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
读法二
package test2;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class T1 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("D:\\大二上资料\\java高编\\实验\\test.html");
String word = "新浪";
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()));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
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();
}
}
}
}
}