??
字號:
第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 + -