<%@ attribute name=\<%@ attribute name=\<%@ attribute name=\<%
File f=new File(fileDir,fileName); try{
FileOutputStream output=new FileOutputStream(f); byte bb[]=fileContent.getBytes(); output.write(bb,0,bb.length); output.close();
out.println(\文件写入成功!\
out.println(\文件所在目录:\ out.println(\文件的名字:\ }
catch(IOException e) {
out.println(\文件写入失败\ } %>
ReadTag.tag
<%@ tag pageEncoding=\<%@ tag import=\
<%@ attribute name=\<%@ attribute name=\<%
File dir=new File(fileDir); File f=new File(dir,fileName); try {
FileInputStream in=new FileInputStream(f); int m=-1;
byte bb[]=new byte[1024]; String content=null;
while((m=in.read(bb))!=-1) {
content=new String(bb,0,m); out.println(content); }
in.close(); }
catch(IOException e) {
out.println(\文件读取失败\ }
%>
giveContent.jsp页面
writeContent.jsp页面
lookContent.jsp页面
readContent.jsp页面
五、实验结果分析
1、把输入流的指向称做源,程序从指向源的输入流中读取源中的数据。而输出流的指向是数据要去的一个目的地,程序通过向输出流中写入数据把信息传递到目的地。所有字节输入流类都是InputStream(输入流)抽象类的子类,而所有字节输出流都是OutputStream(输出流)抽象类的子类。
2、int read(): 输入流调用该方法从源中读取单个字节的数据,该方法返 回字节值,如果未读出字节就返回-1。
3、void write(int n): 输出流调用该方法向输出流写入单个字节
实验2 使用文件字符流加密文件
一、实验目的
本实验的目的是掌握使用文件字符输入、输出流读写文件。 二、实验要求
编写3个JSP页面inputContent.jsp、write.jsp和read.jsp,两个Tag文件Write.tag和Read.tag。 三、实验内容
1)inputContent.jsp页面提供一个表单,要求该表单提供TextArea的输入界面输入多行文本提交给write.jsp
2)write.jsp页面调用一个Tag文件Write.tag将inputContent.jsp页面提交的文本信息加密后写入到文件save.txt中。
3)read.jsp页面提供一个表单,该表单提供两个单选按钮,名字分别是“读取加密文件”,该页面选中的单选按钮的值提交给本页面。
4)Write.tag文件使用attribute指令获得write.jsp页面传递过来的文本信息,并使用文件输出流将其写入到文件save.txt。
5)Read.tag文件使用文件输入输出流读取文件save.txt,并根据read.jsp的要求决定是否进行解密处理,然后使用variable指令将有关信息返回给read.jsp页面。 四、实验结果: 实验所用代码如下:
inputContent.jsp
<%@ page contentType=\<%@ taglib tagdir=\
write.jsp
<%@ page contentType=\<%@ taglib tagdir=\
<%String str=request.getParameter(\ if(str.length()>0)
{
byte bb[]=str.getBytes(\ str=new String(bb);
%>
read.jsp
<%@ page contentType=\<%@ taglib tagdir=\
<%
String condition=request.getParameter(\ if(condition!=null) { %>
<% } %> Write.tag <%@ variable name-given=\<%@ tag pageEncoding=\<%@ tag import=\ <%@ attribute name=\ <% File dir=new File(\ dir.mkdir(); File f=new File(dir,\ try { FileWriter outfile=new FileWriter(f); BufferedWriter bufferout=new BufferedWriter(outfile); char a[]=content.toCharArray(); for(int i=0;i jspContext.setAttribute(\文件加密成功\ } catch(IOException e) { jspContext.setAttribute(\文件加密失败\ } %> Read.tag <%@ tag pageEncoding=\<%@ tag import=\ <%@ attribute name=\ <%@ variable name-given=\<% File dir=new File(\ File f=new File(dir,\ StringBuffer mess=new StringBuffer(); String str; try { FileReader in=new FileReader(f) ; BufferedReader bufferin=new BufferedReader(in); String temp; while((temp=bufferin.readLine())!=null) mess.append(temp); bufferin.close(); in.close(); str=new String(mess); if(method.equals(\ jspContext.setAttribute(\ else if(method.equals(\ {