本文是根据tomcat平台下实现而做,文件目录为:
tom_home\webapps\news下:
└html
└WEB-INF
└classes
└com
└FileMan.class
└FileServlet.class
└web.xml
首页我们先实现文件读取的类:FileMan.java
//FileMan.java 读写文件的一个类
package com;
import java.io.*;
public class FileMan{
private String currentRecord = null;//保存文本的变量
private BufferedReader file; //BufferedReader对象,用于读取文件数据
private String path;//文件完整路径名
public FileMan() {
}
//ReadFile方法用来读取文件filePath中的数据,并返回这个数据
public String ReadFile(String filePath) throws FileNotFoundException
{
path = filePath;
//创建新的BufferedReader对象
file = new BufferedReader(new FileReader(path));
String returnStr =null;
try
{
//读取一行数据并保存到currentRecord变量中
currentRecord = file.readLine();
}
catch (IOException e)
{//错误处理
System.out.println("读取数据错误.");
}
if (currentRecord == null)
//如果文件为空
returnStr = "没有任何记录";
else
{//文件不为空
returnStr =currentRecord;
}
//返回读取文件的数据
return returnStr;
}
//写入文件
public void WriteFile(String filePath,String tempcon) throws FileNotFoundException
{
path = filePath;
try {
//创建PrintWriter对象,用于写入数据到文件中
PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
//用文本格式打印整数Writestr
pw.println(tempcon);
//清除PrintWriter对象
pw.close();
} catch(IOException e) {
//错误处理
System.out.println("写入文件错误"+e.getMessage());
}
}
/*下面这一般你可以用来测试java应用程序来读取文件,将前面的"//"去掉后你可以运行:java FileMan 来测试。*/
//public static void main(String args)
//{
//FileMan fm=new FileMan();
//try
//{
//fm.WriteFile("test.txt","asf");
//}
//catch(FileNotFoundException e){}
//}
}
tom_home\webapps\news下:
└html
└WEB-INF
└classes
└com
└FileMan.class
└FileServlet.class
└web.xml
接着我们先实现servlet:FileServlet .java
/*
* FileServlet.java
*
* Created on 2005年6月19日, 下午3:03
*/
package com;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.PageContext;
/**
*
* @author 淘特网
* @version
*/
public class FileServlet extends HttpServlet {
ServletContext sc;
/** Initializes the servlet.
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
sc=config.getServletContext();
}
/** Destroys the servlet.
*/
public void destroy() {
}
/** Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//* TODO output your page here
out.println("");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("Hello");
out.println(sc.getRealPath("/"));
FileMan fm=new FileMan();
try
{
fm.WriteFile(sc.getRealPath("/")+"/html/test.htm","asf");
out.println(fm.ReadFile(sc.getRealPath("/")+"/html/test.htm"));
}
catch(FileNotFoundException e){}
out.println("");
out.println("");
// */
out.close();
}
/** Handles the HTTP GET method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
}
tom_home\webapps\news下:
└html
└WEB-INF
└classes
└com
└FileMan.class
└FileServlet.class
└web.xml
下面开始修改web.xml令filesevelet生效/*
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<!-- JSPC servlet mappings start -->
<servlet>
<servlet-name>org.apache.jsp.index_jsp</servlet-name>
<servlet-class>org.apache.jsp.index_jsp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>org.apache.jsp.index_jsp</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>FileServlet</servlet-name>
<servlet-class>com.FileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileServlet</servlet-name>
<url-pattern>/servlet/FileServlet</url-pattern>
</servlet-mapping>
<!-- JSPC servlet mappings end -->
</web-app>
在浏览器中输入:http://localhost:8080/news/servlet/FileServlet
OK,出现以下面面:
同时 在news\html\下可以找到test.htm,OK,成功!