文件Example3_1.jsp是可读的吗?文件Example3_1.jsp的长度:字节jasper.sh是目录吗?Example3_1.jsp的父目录是:" />
文档库 最新最全的文档下载
当前位置:文档库 › 第4章(1)文件

第4章(1)文件

第4章 文件
例子1(效果如图4.1所示)
Example4_1.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.io.*"%>


<%File f1=new
File("D:\\Tomcat\\jakarta-tomcat-4.0\\webapps\\root","Example3_1.jsp");
File f2=new File("jasper.sh");
%>

文件Example3_1.jsp是可读的吗?
<%=f1.canRead()%>


文件Example3_1.jsp的长度:
<%=f1.length()%>字节


jasper.sh是目录吗?
<%=f2.isDirectory()%>


Example3_1.jsp的父目录是:
<%=f1.getParent()%>


jasper.sh的绝对路径是:
<%=f2.getAbsolutePath()%>




例子2(效果如图4.2所示)
Example4_2.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.io.*"%>


<% File dir=new
File("D:/Tomcat/jakarta-tomcat-4.0/webapps/root","Students");
%>

在root下创建一个新的目录:Student,
成功创建了吗?
<%=dir.mkdir()%>

Student是目录吗?
<%=dir.isDirectory()%>




例子3(效果如图4.3所示)
Example4_3.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.io.*"%>


<% File dir=new File("D:/Tomcat/jakarta-tomcat-4.0/webapps/root");
File file[]=dir.listFiles();
%>

列出root下的5个长度大于1000字节的文件和全部目录:

目录有:
<% for(int i=0;i{if(file[i].isDirectory())
out.print("
"+file[i].toString());
}
%>

5个长度大于1000 字节的文件名字:
<% for(int i=0,number=0;(i{if(file[i].length()>=1000)
{out.print("
"+file[i].toString());
number++;
}
}
%>




例子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);
}
}
%>

下面列出了服务器上的一些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.*" %>


<%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");
b

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.*" %>


<%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[]="你们好,很高兴认识你们呀!
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)
{
}
%>



例子7(效果如图4.8所示)
Example4_7.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>


<%
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("
"+str);
}
bufferin.close();
in.close();
}
catch(IOException e)
{
}
%>



例子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);
BufferedRe

ader buffer=new BufferedReader(in);
while((str=buffer.readLine())!=null)
{i++;
session.setAttribute(""+i,str);
}
}
catch(IOException e)
{
}
%>



点击按钮进入练习页面:







Exercise.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<%@ page import ="java.util.*" %>


<% 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("
"+"试题"+number+"
"+option[0]);
out.print("
请选择您的答案:");
out.print("
");

out.print("
"+"");
out.print("A. "+option[1]);
out.print("
"+"");
out.print("B. "+option[2]);
out.print("
"+"");
out.print("C. "+option[3]);
out.print("
"+"");
out.print("D. "+option[4]);
out.print("
"+"");
out.print("
");
}
%>
<% 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 Intege

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.*" %>


<%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("
");
}
else
{out.print(new String(b));
}
}
push.close();
}
catch(IOException e){}
%>


例子10(效果如图4.11、4.12所示)
Example4_10.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>


在下面的表格输入成绩:








<% int i=0;
while(i<=6)
{out.print("");
out.print("");

out.print("

");
out.print("");
out.print("");
i++;
}
%>





姓名 数学英语
");
out.print("");
out.print("
");
out.print("");
out.print("
");
out.print("");
out.print("


Math English


<%
String name[]=request.getParameterValues("name");
String math[]=request.getParameterValues("math");
String english[]=request.getParameterValues("english");
try{ File f=new File("f:/2000","student.txt");
FileOutputStream o=new FileOutputStream(f);
DataOutputStream DataOut=new DataOutputStream(o);
for(int k=0;k{DataOut.writeUTF(name[k]);
DataOut.writeUTF(math[k]);
DataOut.writeUTF(english[k]);
}
DataOut.close();
o.close();
}
catch(IOException e)
{ }
catch(NullPointerException ee)
{ }
%>


查看成绩单:

连接到成绩单页面>



showresult.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>


成绩单:
<% try{ File f=new File("f:/2000","student.txt");
FileInputStream in=new FileInputStream(f);
DataInputStream DataIn=new DataInputStream(in);
String name="ok";
String math="0",english="0";
out.print("

");
out.print("
");
out.print("
");
out.print("
");
out.print("
");
out.print("
");
while((name=DataIn.readUTF())!=null)
{ byte bb[]=name.getBytes("ISO-8859-1");
name=new String(bb);
math=DataIn.readUTF();
english=DataIn.readUTF();
out.print("
");
out.print("
");
out.print("
");
out.print("
");
out.print("
");
}
out.print("
姓名 数学英语
");
out.print(name);
out.print("
");
out.print(math);
out.print("
");
out.print(english);
out.print("
");
DataIn.close(); in.close();
}
catch(IOException ee)
{ }
%>



例子11(效果如图4.13、4.14所示)
Example4_11.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<%@ page import ="java.util.*" %>


输入货物有关

信息:


货号:

数量:






查看库存情况







input.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<%@ page import ="java.util.*" %>


<%! //声明创建一个散列表对象,被所有的客户共享:
Hashtable hashtable=new Hashtable();
//一个向散列表填加信息的同步方法:
synchronized void putGoodsToHashtable(String key,String list)
{hashtable.put(key,list);
}
//从散列表移去信息的同步方法:
synchronized void removeGoodsToHashtable(String key)
{hashtable.remove(key);
}
%>
<%--获取客户提交的书号名字和数量--%>
<% String name=request.getParameter("N");
if(name==null)
{name="have no any goods number";
}
byte c[]=name.getBytes("ISO-8859-1");
name=new String(c);
String mount=request.getParameter("M");
if(mount==null)
{mount="have no any recoder";
}
byte d[]=mount.getBytes("ISO-8859-1");
mount=new String(d);
%>
<%--从文件中读散列表,如果文件不存在,你就是第一个录入的人,负责写散列表到文件--%>
<%File f=new File("F:/2000","goods_name.txt");
if(f.exists())
{ try{FileInputStream in=new FileInputStream(f);
ObjectInputStream object_in=new ObjectInputStream(in);
hashtable=(Hashtable)object_in.readObject();
object_in.close();
in.close();
String temp=(String)hashtable.get(name);
if(temp==null)
{temp="";
}
if((temp.length())!=0)
{ out.print("
"+"该货号已经存在,您已经修改了数量");
String s="货号:"+name+" ,数量:"+mount;
removeGoodsToHashtable(name); //首先移去旧的信息。
putGoodsToHashtable(name,s); //填加新的信息。
//将新的散列表写入到文件:
try{FileOutputStream o=new FileOutputStream(f);
ObjectOutputStream object_out=new ObjectOutputStream(o);
object_out.writeObject(hashtable);
object_out.close();
o.close();
}
catch(Exception eee)
{ }
}
else
{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();
}
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)
{ }
}
%>


查看库存情况




返回录入页面



showgoods.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<%@ page import ="java.util.*" %>


已有货物的有关信息:
<%
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");
%>

选择您想续写小说的名字:



美丽的故事

火热的夏天

秋天的收获

冬天的大雪






continueWrite.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>


小说已有内容:

<%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){}
%>

请输入续写的新内容:








continue.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<%@ page import ="java.util.*" %>


<%! //声明一个需同步处理的文件:
File f=null;
String use="yes";
%>
<%--获取客户提交的小说的名字--%>
<% String name=(String)session.getAttribute("name");
byte c[]=name.getBytes("ISO-8859-1");
name=new String(c);
//获取客户续写的内容:
String content=(String)request.getParameter("messages");
if(content==null)
{content=" ";
}
%>
<%File storyFileDir=new File("F:/2000","story");
storyFileDir.mkdir();
f=new File(storyFileDir,name);
//把对文件的操作放入一个同步块中,并通知
//其它用户该文件正在被操作中:
if(use.startsWith("yes"))
{ synchronized(f)
{ use="beisusing";
try{
RandomAccessFile file=new RandomAccessFile(f,"rw");
file.seek(file.length());

//定位到文件的末尾。
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" %>


选择要上传的文件:









accept.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>


<%try{ InputStream in=request.getInputStream();
File f=new File("F:/2000","B.txt");
FileOutputStream o=new FileOutputStream(f);
byte b[]=new byte[1000];
int n;
while((n=in.read(b))!=-1)
{o.write(b,0,n);
}
o.close();
in.close();
}
catch(IOException ee){}
out.print("文件已上传");
%>



例子14(效果如图4.21、4.22、4.23所示)
Example4_14.jsp:
<%@ page contentType="text/html;charset=GB2312" %>


<% String str=response.encodeURL("acceptFile.jsp");
%>

选择要上传的文件:








acceptFile.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>


<%try{ //用客户的session的id建立一个临时文件:
String tempFileName=(String)session.getId();
//建立临时文件f1:
File f1=new File("D:/Tomcat/jakarta-tomcat-4.0/webapps/examples/",tempFileName);
FileOutputStream o=new FileOutputStream(f1);
//将客户上传的全部信息存入f1:
InputStream in=request.getInputStream();
byte b[]=new byte[10000];
int n;
while( (n=in.read(b))!=-1)
{o.write(b,0,n);
}
o.close();in.close();
//读取临时文件f1,从中获取上传文件的名字,和上传的文件的内容:
RandomAccessFile random=new RandomAccessFile(f1,"r");
//读出f1的第2行,析取出上传文件的名字:
int second=1;
String secondLine=null;
while(second<=2)
{secondLine=random.readLine();
second++;

}
//获取第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{ n=random.readByte();
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" %>


点击超链接下载Zip文档book.Zip

下载book.zip



loadFile.jsp:
<%@

page contentType="text/html;charset=GB2312" %>
<%@ 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行:





点击按钮读重新读取文件: