封装一类由 BufferedReader 和 BufferedWriter 类实现文件读取和写出功能,按行读取英文
文本文件 EnglishInit.txt,并在该行的尾部填上该英语句子中含有的单词数目,然后再将该行
保存到 EnglishCount.txt 文件中。EnglishInit.txt 文件预先保存在与当前类文件在同一目录下
且有适当的内容。
File file=new File("EnglishInit.txt");
File file1=new File("EnglishCount.txt");
English b=new English();
b.read(file);
b.read(file1);
package newpackage;
import java.io.*;
import java.util.StringTokenizer;
/**
*
* @author LENONO
*/
public class English {
String[] content={"hello world"};
String[] content1={"how are you"};
String[] content2={"I am fine thank you"};
String str=null;
BufferedReader r;
BufferedWriter w;
public void read(File file){
try{
w=new BufferedWriter(new FileWriter(file));
for(String s:content){
w.write(s);
w.newLine();
}
for(String s1:content1){
w.write(s1);
w.newLine();
}
for(String s2:content2){
w.write(s2);
w.newLine();
}
w.close();
r=new BufferedReader(new FileReader(file));
while((str=r.readLine())!=null){
int sum=0;
char[] ch=str.toCharArray();
for(char c:ch){
if(c==' '){
sum++;
}
}
System.out.println(str+" "+(sum+1));
}
}catch(IOException ioe){
}
}
}