盾怪网教程:是一个免费提供流行杀毒软件教程、在线学习分享的学习平台!

JSP中读文件与写文件的例子

时间:2024/12/21作者:未知来源:盾怪网教程人气:

[摘要]<%@ page import="java.io.*" %><html> <head> <title>Lion互动网络==...
<%@ page import="java.io.*" %>

<html>
  <head>
    <title>Lion互动网络==》JSP中读文件和写文件的例子</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>

关键词:JSP中读文件与写文件的例子




Copyright © 2012-2018 盾怪网教程(http://www.dunguai.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版