第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=su