jsp输入html标签

来源:互联网 发布:黄磊女儿 知乎 编辑:程序博客网 时间:2024/05/01 18:07
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("&nbsp;");
continue;
}
if(c == '<')
{
sb.append("&lt;");
continue;
}
if(c == '>')
{
sb.append("&gt;");
continue;
}
if(c == '/n'){
sb.append("<br> ");
continue;
}
if(c == '&' ){
sb.append("&amp;");
continue;
}
else
sb.append(c);
}
return sb.toString();
}
public static String transform(String content)
{
content=content.replaceAll("&","&amp;");
content=content.replaceAll("<","&lt;");
content=content.replaceAll(" ","&nbsp;");
content=content.replaceAll(">","&gt;");
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);
*发生内存溢出错误
*/
}
}
原创粉丝点击