回 帖 发 新 帖 刷新版面

主题:[讨论]又一个程序问题??很奇怪!!进来看看

<%@ page contentType="text/htmlcharset=GBK" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>

<%!            //对字符串进行压流处理的方法 
   public String getString(String content){
      try{
         StringReader in=new StringReader(content);
         PushbackReader push=new PushbackReader(in);
         StringBuffer stringbuffer=new StringBuffer();
         int c;
         char b[]=new char[1];
         while((c=push.read(b,0,1))!=-1){
            String s=new String(b);
            if(s.equals("<")){
               push.unread('&');
               push.read(b,0,1);
               stringbuffer.append(new String(b));
               push.unread('L');
               push.read(b,0,1);
               stringbuffer.append(new String(b));
               push.unread('T');
               push.read(b,0,1);
               stringbuffer.append(new String(b));
            }else if(s.equals(">")){
               push.unread('&');
               push.read(b,0,1);
               stringbuffer.append(new String(b));
               push.unread('G');
               push.read(b,0,1);
               stringbuffer.append(new String(b));
               push.unread('T');
               push.read(b,0,1);
               stringbuffer.append(new String(b));
            }else if(s.equals("/n")){
               stringbuffer.append("<BR>");
            }else{
               stringbuffer.append(s);
            }
         }
         push.close();
         in.close();
         return new String(stringbuffer);    //返回处理后的字符串
      }catch(IOException e1){
         return new String("不能读取内容");
      }
  }
%>

<%
  File file=null;
  FileReader in=null;
  BufferedReader buffer=null;
  if(session.isNew()){
     file=new File("C:/Tomcat 5.0/work/Catalina/localhost/MyJsp/org/apache/jsp","A.txt");
     in=new FileReader(file);
     buffer=new BufferedReader(in);
     session.setAttribute("file",file);
     session.setAttribute("FileReader",in);
     session.setAttribute("BufferedReader",buffer);
  }
  String s=request.getParameter("reRead");
  if(s==null){
     s="";
  }
  if(s.equals("重新读取文件")){
     file=new File("C:/Tomcat 5.0/work/Catalina/localhost/MyJsp/org/apache/jsp","A.txt");
     in=new FileReader(file);
     buffer=new BufferedReader(in);
     session.setAttribute("file",file);
     session.setAttribute("FileReader",in);
     session.setAttribute("BufferedReader",buffer);
  }
  try{
     String str=null;
     int i=1;
     file=(File)session.getAttribute("file");
     in=(FileReader)session.getAttribute("FileReader");
     buffer=(BufferedReader)session.getAttribute("BufferedReader");
     while((str=buffer.readLine())!=null&&i<=5){
        str=getString(str);
        out.print("<BR>"+str);
        i++;
     }
  }catch(IOException e){
     out.print("该文件不存在");
  }
%>
<html>
<body>
<p>单击按钮读取下五行:</p>
<form action="ReadFileByLine.jsp" method="post" name="form">
   <input type="submit" name="next5" value="读取文件的下五行">
</form>
<form action="ReadFileByLine.jsp" method="post" name="form1">
   <input type="submit" name="reRead" value="重新读取文件">
</form>
</body>
</html>
在IE中打开后弹出文件下载的对话框(询问是否打开或保存文件)当点击找开后,程序被默认的(Jcreator)打开!,不语~~~~~~~~~~~程序是按书上打的!

回复列表 (共2个回复)

沙发

<%@ page contentType="text/htmlcharset=GBK" %>
少了东西拉,知道少了;这个没有

板凳

xiexie

我来回复

您尚未登录,请登录后再回复。点此登录或注册