Java字符流读取文档,统计字节数,计算字符串出现次数,字符串代替,另存文档

使用字符流相关类编写程序J_ReadTxt.java,读取《柳永宋词.txt》,然后统计文档的字节数,字符串“柳永”出现的次数;并用字符串“景庄”代替文档中的所有“柳永”字符串,并另存到《景庄婉约.txt》文档中。

package p1;

import java.io.FileReader;
import java.io.FileWriter;

public class J_ReadTxt
{
private static final String LIUYONG = "柳永宋词.txt";
private static final String JINGZHUANG = "景庄婉约.txt";

public static void main ( String[] args )
{
char[] cs = new char[1];
int count = 0;
String result = "";
try
{
FileReader fr = new FileReader (LIUYONG);
while (-1 != fr.read (cs))
{
String temp = String.valueOf (cs);
result += temp;
int b = temp.codePointAt (0);
if (b > 127)
{
count += 2;
}
else
{
count++;
}
}
fr.close ();
System.out.println ("文档的字节数: " + count);
String temp = " " + result + " ";
System.out.println ("字符串“柳永”出现的次数: " + ( temp.split ("\u67f3\u6c38").length - 1 ));
result = result.replaceAll ("\u67f3\u6c38", "\u666f\u5e84");
FileWriter fw = new FileWriter (JINGZHUANG);
fw.write (result);
fw.flush ();
fw.close ();
}
catch (Exception e)
{
e.printStackTrace ();
}
}
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-12-06
提示:inputStream
replaceAll(),,
相似回答