




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
本文是根据tomcat平台下实现而做,文件目录为:tom_homewebappsnews下:htmlWEB-INFclasses comFileMan.classFileServlet.classweb.xml首页我们先实现文件读取的类:FileMan.java/FileMan.java 读写文件的一个类package com;import java.io.*;public class FileManprivate String currentRecord = null;/保存文本的变量private BufferedReader file; /BufferedReader对象,用于读取文件数据private String path;/文件完整路径名public FileMan() /ReadFile方法用来读取文件filePath中的数据,并返回这个数据public String ReadFile(String filePath) throws FileNotFoundExceptionpath = 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 FileNotFoundExceptionpath = filePath;try /创建PrintWriter对象,用于写入数据到文件中PrintWriter pw = new PrintWriter(new FileOutputStream(filePath);/用文本格式打印整数Writestrpw.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)/接着我们先实现servlet:FileServlet .java/* FileServlet.java*/package com;import java.io.*;import .*;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.PageContext;/*/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();tryfm.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; 下面开始修改web.xml令filesevelet生效/* Welcome to Tomcat Welcome to Tomcat org.apache.jsp.index_jsp org.apache.jsp.index_jsp org.apache.jsp.index_jsp
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 青岛功能区管理办法
- 经销商惩戒管理办法
- 《手机娱乐管理办法》
- 专利出口登记管理办法
- 中国工会资产管理办法
- xx资金使用管理办法
- 衡阳景区公厕管理办法
- 车间设备维护保养管理办法
- 2025年眼科医疗器械技术创新趋势与市场潜力挖掘策略研究报告
- 企业研发新产品试制协议
- 暂住人员管理办法
- 护理查对不良事件案例分析
- 化验室精细化管理
- 洗衣服劳动与技术课件
- 无人机地形测量方案
- 2024年呼伦贝尔农垦集团有限公司招聘笔试真题
- 安防报警信息系统项目投资可行性研究分析报告(2024-2030版)
- 人教版三年级上数学第二单元《含括号的混合运算》课时练习卷(含答案)
- 湖南美术出版社二年级美术上册学期教学计划
- 医疗器械培训计划和记录
- 2025年上海市中考语文试题含解析
评论
0/150
提交评论