⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄

📁 清华版的jsp基础程序 对新手很有用
💻
📖 第 1 页 / 共 3 页
字号:
第4章 文件
例子1(效果如图4.1所示)
Example4_1.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.io.*"%> 
<HTML>
<BODY bgcolor=cyan><Font Size=1>
  <%File f1=new
     File("D:\\Tomcat\\jakarta-tomcat-4.0\\webapps\\root","Example3_1.jsp");
     File f2=new File("jasper.sh");
  %>
 <P> 文件Example3_1.jsp是可读的吗?
    <%=f1.canRead()%>
 <BR>   
 <P>文件Example3_1.jsp的长度:
    <%=f1.length()%>字节
 <BR>
 <P> jasper.sh是目录吗?
    <%=f2.isDirectory()%>
 <BR>
 <P>Example3_1.jsp的父目录是:
    <%=f1.getParent()%>
 <BR>
 <P>jasper.sh的绝对路径是:
    <%=f2.getAbsolutePath()%>
</Font> 
</BODY>
</HTML>

例子2(效果如图4.2所示)
Example4_2.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.io.*"%> 
<HTML>
<BODY><Font Size=2>
  <% File dir=new 
File("D:/Tomcat/jakarta-tomcat-4.0/webapps/root","Students");
   %>
 <P> 在root下创建一个新的目录:Student,<BR>成功创建了吗?
    <%=dir.mkdir()%>
 <P> Student是目录吗?
    <%=dir.isDirectory()%>
</Font> 
</BODY>
</HTML>

例子3(效果如图4.3所示)
Example4_3.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.io.*"%> 
<HTML>
<BODY><Font Size=2>
  <% File dir=new File("D:/Tomcat/jakarta-tomcat-4.0/webapps/root");
         File file[]=dir.listFiles();
   %>
 <P> 列出root下的5个长度大于1000字节的文件和全部目录:
  <BR>目录有:
     <% for(int i=0;i<file.length;i++)
            {if(file[i].isDirectory())
                out.print("<BR>"+file[i].toString());
            }
     %>
 <P> 5个长度大于1000 字节的文件名字:
     <% for(int i=0,number=0;(i<file.length)&&(number<=5);i++)
            {if(file[i].length()>=1000)
                {out.print("<BR>"+file[i].toString());
                  number++;
                }
            }
     %>
</Font> 
</BODY>
</HTML>

例子4
Example4_4.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<%! class FileJSP implements FilenameFilter 
     { String str=null;
         FileJSP(String s)
         {str="."+s;
         }
    public  boolean accept(File dir,String name)
         { return name.endsWith(str);
         }              
     }
%>
<P>下面列出了服务器上的一些jsp文件
 <% File dir=new File("d:/Tomcat/Jakarta-tomcat-4.0/webapps/root/");
   FileJSP file_jsp=new FileJSP("jsp");
   String file_name[]=dir.list(file_jsp);
    for(int i=0;i<5;i++)
           {out.print("<BR>"+file_name[i]);
           }
 %>

例子5(效果如图4.4所示)
Example4_5.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<HTML>
<BODY>
  <%File f=new File("d:/Tomcat/Jakarta-tomcat-4.0/webapps/root/","A.java");
   File dir=new File("d:/Tomcat/Jakarta-tomcat-4.0/webapps/root","Students");
    boolean b1=f.delete();
    boolean b2=dir.delete();
  %>
<P>文件A.java成功删除了吗?
   <%=b1%>
<P>目录Students成功删除了吗?
   <%=b2%>
</BODY>
</HTML>

例子6(效果如图4.7所示)
Example4_6.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<HTML>
<BODY bgcolor=cyan><FONT size=1>
  <%File dir=new 
File("d:/Tomcat/Jakarta-tomcat-4.0/webapps/root","Students");
     dir.mkdir();
    File f=new File(dir,"hello.txt");
    try{
        FileOutputStream  outfile=new FileOutputStream(f);
        BufferedOutputStream bufferout=new BufferedOutputStream(outfile);
        byte b[]="你们好,很高兴认识你们呀!<BR>nice to meet you".getBytes();
        bufferout.write(b);
        bufferout.flush();
        bufferout.close();
        outfile.close();
        FileInputStream  in=new FileInputStream(f);
        BufferedInputStream bufferin=new BufferedInputStream(in);
        byte c[]=new byte[90];
        int n=0;
          while((n=bufferin.read(c))!=-1)
               { String temp=new String(c,0,n);
                 out.print(temp); 
               }
        bufferin.close();
         in.close();
       }
     catch(IOException e)
       {
       }
  %>
</BODY>
</HTML>

例子7(效果如图4.8所示)
Example4_7.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<HTML>
<BODY>
  <%
File dir=new File("d:/Tomcat/Jakarta-tomcat-4.0/webapps/root","Students");
    dir.mkdir();
    File f=new File(dir,"apple.txt");
    try{FileWriter  outfile=new FileWriter(f);
        BufferedWriter bufferout=new BufferedWriter(outfile);
        bufferout.write("你好:");
        bufferout.newLine();
        bufferout.write("好久不见了,近来在忙什么呢?");
        bufferout.newLine();
        bufferout.write("欢迎来玩");
        bufferout.newLine();
        bufferout.write("2002年8月8日");
        bufferout.flush();
        bufferout.close();
        outfile.close();
        FileReader in=new FileReader(f);
        BufferedReader bufferin=new BufferedReader(in);
        String str=null;  
           while((str=bufferin.readLine())!=null)
               {out.print("<BR>"+str); 
               }
         bufferin.close();
         in.close();
       }
     catch(IOException e)
       {
       }
  %>
</BODY>
</HTML>

例子8(效果如图4.9所示)
Example4_8.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<%@ page import ="java.util.*" %>
  <% int i=0;
     String str=null;
     Integer score=new Integer(0); 
     Integer number=new Integer(0); 
     session.setAttribute("score",score);
     session.setAttribute("序号",number);
        try{ //englishi.txt存放在f:/2000目录下。
             File f=new File("f:/2000","English.txt");
             FileReader in=new FileReader(f);
             BufferedReader buffer=new BufferedReader(in);
             while((str=buffer.readLine())!=null)
               {i++;
                session.setAttribute(""+i,str);
               }
           }
        catch(IOException e)
          {
          }
   %>
<HTML>
<BODY>
 <P><BR>点击按钮进入练习页面:
 <FORM action="Exercise.jsp" method="post" name=form>
    <Input type=submit value="开始练习">
 </FORM>  
</BODY>
</HTML>

Exercise.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<%@ page import ="java.util.*" %>
<HTML>
<BODY>
  <%  String option[]=new String[7];
      int 题号=0;
      if(!(session.isNew()))
      {  Integer number=(Integer)session.getAttribute("序号");//获取题号。
                 if(number==null)
                    {number=new Integer(0);
                    }
                 number=new Integer(number.intValue()+1);//将题号加1。
         session.setAttribute("序号",number);            //更新序号
                int i=0;
        String str=(String)session.getAttribute(""+number);//获取行号是number的文本。
           if(str==null)
             {str="#练习结束#练习结束#练习结束#练习结束#练习结束#再见#";
             }
        StringTokenizer tokenizer=new StringTokenizer(str,"#");//分析该行文本。 
          while(tokenizer.hasMoreTokens())
               {option[i]=tokenizer.nextToken();i++;
               }
          题号=number.intValue();                       
          session.setAttribute("答案"+题号,option[5]);   //将该题答案存入session。
          out.print("<BR>"+"试题"+number+"<BR>"+option[0]);
          out.print("<BR>请选择您的答案:");
          out.print("<FORM action=Exercise.jsp method=post name=form>");
            
            out.print("<BR>"+"<Input type=radio name=R value=A>");
            out.print("A. "+option[1]);
            out.print("<BR>"+"<Input type=radio  name=R value=B>");
            out.print("B. "+option[2]);
            out.print("<BR>"+"<Input type=radio  name=R value=C>");
            out.print("C. "+option[3]);
            out.print("<BR>"+"<Input type= radio name=R value=D>");
            out.print("D. "+option[4]); 
            out.print("<BR>"+"<Input type=submit name=submit value=提交答案>");
            out.print("</FORM>");
       }
   %>
   <% String answer=request.getParameter("R");//获取客户提交的答案。  
       //获取题目的标准答案,需要注意的是:客户提交答案后,该页面就将题号增加1
     // 因此,要给客户的上一题进行评判必须将题号减1。   
      String 答案=(String)session.getAttribute("答案"+(题号-1)); 
       if(answer==null)
          {answer="您没有给出选择呢";
          }
       if(answer.equals(答案))
          { Integer score=(Integer)session.getAttribute("score");
            score=new Integer(score.intValue()+1);
            session.setAttribute("score",score);
          } 
      out.print("<BR>"+"您现在的得分是:"+session.getAttribute("score"));
      out.print("<BR>"+"你的上一题的选择是:"+answer);
      out.print("<BR>"+"上一题的正确答案是:"+答案);
    %>       
</BODY>
</HTML>   

例子9(效果如图4.10所示)
Example4_9.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<HTML>
<BODY bgcolor=cyan><FONT size=1>
  <%File f=new File("d:/Tomcat/Jakarta-tomcat-4.0/webapps/root","Example2_4.jsp");
    try{ FileReader in=new FileReader(f) ;
        PushbackReader push=new PushbackReader(in);
        int c; 
        char b[]=new char[1];              
        while ( (c=push.read(b,0,1))!=-1)//读取1个字符放入字符数组b。 
          { String s=new String(b);
              if(s.equals("<"))        //回压的条件  
               {  push.unread('&');
                  push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
                  out.print(new String(b));
                  push.unread('L');
                  push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
                  out.print(new String(b));
                  push.unread('T');
                  push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
                  out.print(new String(b));
               }
            else if(s.equals(">"))        //回压的条件  
               {  push.unread('&');
                  push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
                  out.print(new String(b));
                  push.unread('G');
                  push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
                  out.print(new String(b));
                  push.unread('T');
                  push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
                  out.print(new String(b));
               }
            else if(s.equals("\n"))        
              { out.print("<BR>");
              }
            else
               {out.print(new String(b));
               }
           }
          push.close();
       }
    catch(IOException e){} 
  %>
</BODY>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -