<%@ page import="java.io.*" %>
<html>
<head>
<title>Lion互動網絡==》jsp(SUN企業級應用的首選)中讀文件和寫文件的例子</title>
</head>
<body>
<%
//寫文件
String str = "WWW.LIONSKY.NET";
String filename = request.getRealPath("lionsky.txt");
java.io.File f = new java.io.File(filename);
if(!f.exists())//如果文件不存,則建立
{
f.createNewFile();
}
try
{
PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
pw.println(str);//寫內容
pw.close();
}
catch(IOException e)
{
out.println(e.getMessage());
}
//讀文件
java.io.FileReader fr = new java.io.FileReader(f);
char[] buffer = new char[10];
int length; //讀出的字符數(一個中文為一個字符)
//讀文件內容
out.write(filename+"<br>");
while((length=fr.read(buffer))!=-1)
{
//輸出
out.write(buffer,0,length);
}
fr.close();
%>
</body>
</html>
<%//寫文件String str = "WWW.LIONSKY.NET";String filename = request.getRealPath("lionsky.txt");java.io.File f = new java.io.File(filename);if(!f.exists())//如果文件不存,則建立{ f.createNewFile();}try{ PrintWriter pw = new PrintWriter(new FileOutputStream(filename)); pw.println(str);//寫內容 pw.close();}catch(IOException e){ out.println(e.getMessage());}//讀文件java.io.FileReader fr = new java.io.FileReader(f);char[] buffer = new char[10];int length; //讀出的字符數(一個中文為一個字符)//讀文件內容out.write(filename+"
");while((length=fr.read(buffer))!=-1){ //輸出 out.write(buffer,0,length);}fr.close();%>