编程开发 > JSP > 文章内容

jsp工作总结(3)

2016-10-3编辑:sunny
文件处理: ********************************************************
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="errorPage.jsp" %>
<%@ page import="java.io.*" %><html>
<head><meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>写一个文件</title></head><body>
<p>本网站所在文件夹:<%=request.getRealPath("/")%> 本网页所在文件夹:<%=request.getRealPath("./")%>
  本网页所在文件夹所在的上一级文件夹:<%=request.getRealPath("../")%> </p>
<p> 本网页所在文件夹所在的上上级文件夹:<%=request.getRealPath("../../")%></p>
<p>本网页所在文件夹子文件夹:<%=request.getRealPath("./ckdr/")%></p>
<p>测试<font color="#FF0000">文件列表</font></p>
<%String path=request.getRealPath("./");File f1=new File(path);
File lst[]=f1.listFiles();%><%=path%>文件夹中有以下文件和文件夹:
<%for(int i=0;i<lst.length;i++){%><br><%=lst[i].getName()%>
<%}%><%String sp=System.getProperty("file.separator");//获取文件字符
String fp="f:"+sp+"JSPdata"+sp+"filcontrol"+sp+"writefile.txt";%>
<font color="#FF0000">
<h3>写一个文件例子一:</h3><%String str4 = "print me";
String path5=request.getRealPath("./");
String nameOfTextFile=path5+"\\1.txt";
try { PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
pw.println(str4);pw.println(str4);pw.close();
} catch(IOException e) {out.println(e.getMessage());}%>
文件读取 <%//变量声明
java.lang.String strFileName;     //文件名
java.io.File objFile;             //文件对象
java.io.FileReader objFileReader; //读文件对象
char[] chrBuffer = new char[10];  //缓冲
int intLength;                    //实际读出的字符数(一个中文为一个字符)
strFileName =nameOfTextFile;//设置待读文件名
objFile = new java.io.File(strFileName);//创建文件对象
//判断文件是否存在
if(objFile.exists()){//文件存在
//创建读文件对象
objFileReader = new java.io.FileReader(objFile);//读文件内容
while((intLength=objFileReader.read(chrBuffer))!=-1){//输出
out.write(chrBuffer,0,intLength); }//关闭读文件对象
   objFileReader.close();}else{//文件不存在
out.println("下列文件不存在:"+strFileName);}%>
</font> <p><font color="#FF0000"> 写一个文件例子二:向文件中追加内容: </font></p>
<form name="form1" method="post" action=""><input type="text" name="tarea">
<input type="submit" name="Submit" value="提交"></form>
<%String path6=request.getRealPath("./");
//File f4=new File(path6+"\\apendwrite.txt");
File f5=new File(path6+"\\apendwrite.txt");
//FileWriter fw=new FileWriter(f4);
String str=request.getParameter("tarea");
//fw.write(str);//fw.close();
//RandomAccessFile raf1=new RandomAccessFile(f4,"r");
RandomAccessFile raf2=new RandomAccessFile(f5,"rw");
long len1=raf2.length();long len2=raf2.length();
raf2.seek(len2);long fpt1=0;raf2.writeBytes(str+'\n');
//while(fpt1<len1)//{String str1=raf1.readLine();
//raf2.writeBytes(str+'\n');//fpt1=raf1.getFilePointer();//}
//raf1.close();
raf2.close();%><p><font color="#FF0000">
</font></p><font color="#FF0000"><p>逐行读取:
<%String path4=request.getRealPath("./");
FileReader f1rd=new FileReader(path4+"\\1.txt");
BufferedReader bfrd=new BufferedReader(f1rd);
String lin=bfrd.readLine();while(lin!=null){
out.println(lin+"<br>");lin=bfrd.readLine();}bfrd.close();f1rd.close();%><%
String path2=request.getRealPath("./");String creatPath=path2+"\\ckdr";
File flpt=new File(creatPath);if(flpt.exists())
{flpt.delete();%></p><p> 删除了<%=creatPath%>文件夹 <%}else{
flpt.mkdir();%>创建了<%=creatPath%>文件夹 <%}%></p><%
String path3=request.getRealPath("./");File f2=new File(path3,"\\creat.txt");
if(f2.exists()){f2.delete();%>删除了<%=path3+"creat.txt"%>文件
<%}else{f2.createNewFile();%>创建了<%=path3+"creat.txt"%>文件 <%}%>
</font> </body></html>
jsp中读取和写文件的函数**********************************************
<%!//改写文件的函数
public void writeFile(String filename)throws IOException{
try { PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
pw.println("123456789");pw.close();} catch(IOException e) {}} //读取文件的函数
public String readFile(String filename) throws IOException{
BufferedReader buff=new BufferedReader(new FileReader(filename));
String temp=buff.readLine();buff.close();return temp;}%>
<%String path4=request.getRealPath("./");%>
<%writeFile(path4+"1.txt");%>
<%=readFile(path4+"1.txt")%>
jsp工作总结(1)

热点推荐

登录注册
触屏版电脑版网站地图