JSP中读文件和写文件的例子
<%@ page import="java.io.*" %> <html> //写文件 try //读文件 </body>
<head>
<title>lion互动网络==》(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();
}
{
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();
%>
</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();%>
<%@ page import="java.io.*" %>
<html>
<head>
<title>lion互动网络==》(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();%>
推荐阅读