Posted on 2007-08-15 10:38
hilor 阅读(1623)
评论(0) 编辑 收藏 所属分类:
J2EE
import java.io.*;
public class Test{
public static String changeToHtml(String input)
{
if(input == null || input.length() == 0)
return "";
char c = ' ';
StringBuffer sb = new StringBuffer(input.length());
for(int i = 0; i < input.length(); i++)
{
c = input.charAt(i);
if(c == ' ')
{
sb.append(" ");
continue;
}
if(c == '<')
{
sb.append("<");
continue;
}
if(c == '>')
{
sb.append(">");
continue;
}
if(c == '\n'){
sb.append("<br> ");
continue;
}
if(c == '&' ){
sb.append("&");
continue;
}
else
sb.append(c);
}
return sb.toString();
}
public static String transform(String content)
{
content=content.replaceAll("&","&");
content=content.replaceAll("<","<");
content=content.replaceAll(" "," ");
content=content.replaceAll(">",">");
content=content.replaceAll("\n","<br>");
return content;
}
public static void main(String []args){
BufferedReader bw;
StringBuffer sb=new StringBuffer("");
String ss="";
long l=0;
try{
File file=new File("G:\\novel\\凡尔纳\\海底两万里\\001.HTM");//随意选的文件
System.out.println(file.getPath().toString());
bw=new BufferedReader(new FileReader(file));
while((ss=bw.readLine())!=null){
System.out.println(ss);
sb.append(ss);
}
bw.close();
for(int i=0;i<10;i++) sb.append(sb);//作一个大字串
}catch(IOException e){}
long a=0;
long b=0;
/*输出使用changeToHtml()所用时间
*a=System.currentTimeMillis();
*changeToHtml(sb.toString());
*b=System.currentTimeMillis();
*System.out.println(b-a);
*此处正常显示时间
*/
/*输出使用transform()所用时间
*a=System.currentTimeMillis();
*transform(sb.toString());
*b=System.currentTimeMillis();
*System.out.println(b-a);
*发生内存溢出错误
*/
}
}
如果字串稍小一点,不发生错误,那么即可看出chageToHtml()所用时间比transform()要少得多。