菜鸟看I/O!!

来源:互联网 发布:中国电信云计算 编辑:程序博客网 时间:2024/05/17 01:41

  今天看了jsp中的文件I/O技术,总体来说挺不错的.

  我发现在应用该技术的时候,一定要先写上下面一段代码,

  String thePath = request.getRealPath("/")+"ch11//theFile";

  用来获取该文件的真实路径。

  下面是一段关于文件应用的小程序。

   <%@ page language="java" import="java.util.*,java.io.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'wk.jsp' starting page</title>
   
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
  <%
 String thePath=request.getRealPath("/")+"D://work//";
 File myFile = new File("D://work//","work.txt");
 myFile.createNewFile();
 FileWriter myFileWriter = new FileWriter(myFile);
 myFileWriter.write("hello,you are welcome.");
 myFileWriter.flush();
 myFileWriter.close();
 out.println("写入成功!<br>");
 char[] chrRead =new char[3600];
 FileReader myFileReader = new FileReader(myFile);
 myFileReader.read(chrRead);
 out.println(chrRead);
 myFileReader.close();

 
   %>
  </body>
</html>

 

 


 

原创粉丝点击