文件Example3_1.jsp是可读的吗?文件Example3_1.jsp的长度:字节jasper.sh是目录吗?Example3_1.jsp的父目录是:" />
第4章 文件
例子1(效果如图4.1所示)
Example4_1.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.io.*"%>
文件Example3_1.jsp是可读的吗?
<%=f1.canRead()%>
文件Example3_1.jsp的长度:
<%=f1.length()%>字节
jasper.sh是目录吗?
<%=f2.isDirectory()%>
Example3_1.jsp的父目录是:
<%=f1.getParent()%>
jasper.sh的绝对路径是:
<%=f2.getAbsolutePath()%>
在root下创建一个新的目录:Student,
成功创建了吗?
<%=dir.mkdir()%>
Student是目录吗?
<%=dir.isDirectory()%>
列出root下的5个长度大于1000字节的文件和全部目录: 5个长度大于1000 字节的文件名字:
目录有:
<% for(int i=0;i
out.print("
"+file[i].toString());
}
%>
<% for(int i=0,number=0;(i
{out.print("
"+file[i].toString());
number++;
}
}
%>
下面列出了服务器上的一些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("
"+file_name[i]);
}
%>
例子5(效果如图4.4所示)
Example4_5.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
oolean b1=f.delete();
boolean b2=dir.delete();
%>
文件A.java成功删除了吗?
<%=b1%>
目录Students成功删除了吗?
<%=b2%>
例子6(效果如图4.7所示)
Example4_6.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
ader buffer=new BufferedReader(in);
while((str=buffer.readLine())!=null)
{i++;
session.setAttribute(""+i,str);
}
}
catch(IOException e)
{
}
%>
点击按钮进入练习页面:
r(score.intValue()+1);
session.setAttribute("score",score);
}
out.print("
"+"您现在的得分是:"+session.getAttribute("score"));
out.print("
"+"你的上一题的选择是:"+answer);
out.print("
"+"上一题的正确答案是:"+答案);
%>
例子9(效果如图4.10所示)
Example4_9.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
在下面的表格输入成绩:
姓名 | ");数学 | ");英语 | ");
---|---|---|
"); out.print(name); out.print(" | "); "); out.print(math); out.print(" | "); "); out.print(english); out.print(" | ");
输入货物有关
信息:
散列表填加新的货物信息。
//再将新的散列表写入到文件:
try{FileOutputStream o=new FileOutputStream(f);
ObjectOutputStream object_out=new ObjectOutputStream(o);
object_out.writeObject(hashtable);
object_out.close();
o.close();
}
catch(Exception eee)
{ }
out.print("
"+"您已经将货物存入文件");
out.print("
"+"货物的货号:"+name);
}
}
catch(IOException e) {}
}
else
{ //如果文件不存在,您就是第1个录入信息的人。
String s="货号:"+name+"数量:"+mount;
putGoodsToHashtable(name,s);//放信息到散列表。
//负责创建文件,并将散列表写入到文件:
try{ FileOutputStream o=new FileOutputStream(f);
ObjectOutputStream object_out=new ObjectOutputStream(o);
object_out.writeObject(hashtable);
object_out.close();
o.close();
out.print("
"+"您是第一个录入货物的人");
out.print("
"+"货物的货号:"+name);
}
catch(Exception eee)
{ }
}
%>
已有货物的有关信息:
<%
try{File f=new File("F:/2000","goods_name.txt");
FileInputStream in=new FileInputStream(f);
ObjectInputStream object_in=new ObjectInputStream(in);
Hashtable hashtable=(Hashtable)object_in.readObject();
object_in.close();
in.close();
Enumeration enum=hashtable.elements();
while(enum.hasMoreElements()) //遍历当前散列表。
{ String goods=(String)enum.nextElement();
out.print("
"+"货号及库存:"+goods);
}
hashtable.clear();
}
catch(ClassNotFoundException event)
{ out.println("无法读出");
}
catch(IOException event)
{out.println("无法读出");
}
%>
返回录入页面
例子12(效果如图4.15、4.16、4.17所示)
Example4_12.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<% String str=respons
e.encodeURL("continueWrite.jsp");
%>
选择您想续写小说的名字:
小说已有内容:
<%String str=response.encodeURL("continue.jsp");
%>
<%--获取客户提交的小说的名字--%>
<%String name=(String)request.getParameter("R");
if(name==null)
{name="";
}
byte c[]=name.getBytes("ISO-8859-1");
name=new String(c);
session.setAttribute("name",name);
File storyFileDir=new File("F:/2000","story");
storyFileDir.mkdir();
File f=new File(storyFileDir,name);
//列出小说的内容:
try{ RandomAccessFile file=
new RandomAccessFile(f,"r");
String temp=null;
while((temp=file.readUTF())!=null)
{ byte d[]=temp.getBytes("ISO-8859-1");
temp=new String(d);
out.print("
"+temp);
}
file.close();
}
catch(IOException e){}
%>
请输入续写的新内容:
//定位到文件的末尾。
file.writeUTF(content);
file.close();
use="yes";
out.print("
"+"内容已经写入");
}
catch(IOException e){}
}
}
//如果该小说正在被续写,就通知客户等待:
else
{out.print("该小说正在被续写,请等待");
}
%>
例子13(效果如图4.18、4.19、4.20所示)
Example4_13.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
选择要上传的文件:
} 查看上传的图象效果 点击超链接下载Zip文档book.Zip page contentType="text/html;charset=GB2312" %>
//获取第2行中目录符号'\'最后出现的位置
int position=https://www.wendangku.net/doc/e05125379.html,stIndexOf('\\');
//客户上传的文件的名字是:
String fileName=secondLine.substring(position+1,secondLine.length()-1);
random.seek(0); //再定位到文件f1的开头。
//获取第4行回车符号的位置:
long forthEndPosition=0;
int forth=1;
while((n=random.readByte())!=-1&&(forth<=4))
{ if(n=='\n')
{ forthEndPosition=random.getFilePointer();
forth++;
}
}
//根据客户上传文件的名字,将该文件存入磁盘:
File f2=new File("D:/Tomcat/jakarta-tomcat-4.0/webapps/examples/",fileName);
session.setAttribute("Name",fileName);//供showImage.jsp页面使用。
RandomAccessFile random2=new RandomAccessFile(f2,"rw");
//确定出文件f1中包含客户上传的文件的内容的最后位置,即倒数第6行。
random.seek(random.length());
long endPosition=random.getFilePointer();
long mark=endPosition;
int j=1;
while((mark>=0)&&(j<=6))
{ mark--;
random.seek(mark);
n=random.readByte();
if(n=='\n')
{ endPosition=random.getFilePointer();
j++;
}
}
//将random流指向文件f1的第4行结束的位置:
random.seek(forthEndPosition);
long startPoint=random.getFilePointer();
//从f1读出客户上传的文件存入f2(读取从第4行结束位置和倒数第6行之间的内容)。
while(startPoint
random2.write(n);
startPoint=random.getFilePointer();
}
random2.close();random.close();
f1.delete(); //删除临时文件
}
catch(IOException ee){}
out.print("文件已上传");
%>
<%String str=response.encodeURL("showImage.jsp");
%>
showImage.jsp:
<% String name=(String)session.getAttribute("Name");
if(name==null)
{name="";
}
out.print("
例子15(效果如图4.24、4.25所示)
Example4_15.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
下载book.zip
loadFile.jsp:
<%@
<%@ page import="java.io.*" %>
<% //获得响应客户的输出流:
OutputStream o=response.getOutputStream();
//输出文件用的字节数组,每次发送500个字节到输出流:
byte b[]=new byte[500];
//下载的文件:
File fileLoad=new File("f:/2000","book.zip");
//客户使用保存文件的对话框:
response.setHeader("Content-disposition","attachment;filename="+"book.zip");
//通知客户文件的MIME类型:
response.setContentType("application/x-tar");
//通知客户文件的长度:
long fileLength=fileLoad.length();
String length=String.valueOf(fileLength);
response.setHeader("Content_Length",length);
//读取文件book.zip,并发送给客户下载:
FileInputStream in=new FileInputStream(fileLoad);
int n=0;
while((n=in.read(b))!=-1)
{ o.write(b,0,n);
}
%>
例子16(效果如图4.26所示)
readFileByLine.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<%! //对字符串进行回压流处理的方法:
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)//读取1个字符放入字符数组b。
{ String s=new String(b);
if(s.equals("<")) //回压的条件
{ push.unread('&');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('L');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('T');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
}
else if(s.equals(">")) //回压的条件
{ push.unread('&');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('G');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
push.unread('T');
push.read(b,0,1); //push读出被回压的字符字节,放入数组b.
stringbuffer.append(new String(b));
}
else if(s.equals("\n"))
{ stringbuffer.append("
");
}
else
{ stringbuffer.append(s);
}
}
push.close();
in.close();
return new String(stringbuffer); //返回处理后的字符串。
}
catch(IOException e)
{return content=new String("不能读取内容");
}
}
%>
<% String s=request.getParameter("g"); //获取客户提交的信息(是否重新读取文件)
if(s==null)
{s=""; }
byte b[]=s.getBytes("ISO-8859-1");
s=new String(b);
File f=null;
FileReader in=null;
BufferedReader buffer=null;
if(session.isNew()) //当第一次请求该页面时,建立和文件的输入流连接。
{ f=new File("f:/javabook","JSP教程.txt");
in=new FileReader(f);
buffer=new BufferedReader(in);
session.setAttribute("file",f); session.setAttribute("in",in);
session.setAttribute("buffer",buffer);
}
if(s.equals("重新读取文件")) //当请求重新读取文件时,建立和文件的输入流连接。
{ f=new File("f:/javabook","JSP教程.txt");
in=new FileReader(f);
buffer=new BufferedReader(in);
session.setAttribute("file",f); session.setAttribute("in",in);
session.setAttribute("buffer",buffer);
}
//将上述对象保存到用户的session 对象中:
try{ String str=null; int i=1;
f=(File)session.getAttribute("file");
in=(FileReader)session.getAttribute("in");
buffer=(BufferedReader)session.getAttribute("buffer");
while( ((str=buffer.readLine())!=null)&&(i<=5))
{ //为了能显示原始的HTML文件或JSP文件需使用回压流技术。
str=getString(str);
out.print("
"+str);
i++;
}
}
catch(IOException e)
{ out.print("文件不存在,或读取出现问题");
}
%>
<%String code=response.encodeURL("readFileByLine.jsp");
%>
点击按钮读取下5行:
点击按钮读重新读取文件: