全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
本文是根据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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 公务员考试试卷省直公布试题及答案
- 公务员考试实操试题及答案
- 齐河教师招聘题库及答案
- 2026年云南外事外语职业学院单招职业倾向性考试必刷测试卷附答案
- 2025年湖北省省直事业单位招聘考试真题试卷 公共基础知识完整参考答案详解
- 2025年湖南郴州永兴县竞聘乡镇(街道)所属事业单位工作人员16人参考题库及一套完整答案详解
- 2026年重庆科技职业学院单招职业适应性考试题库汇编
- 2026年莱芜职业技术学院单招职业技能测试题库必考题
- 2026年青海高等职业技术学院单招综合素质考试必刷测试卷及答案1套
- 2025年甘肃省事业单位招聘考试模拟试卷 公共某础知识(三)及一套完整答案详解
- 聊斋志异介绍
- 五年级体育教学质量提升计划
- 楼盘品茶活动方案
- 儿科护理文书书写规范
- 医疗器械培训计划和记录
- JG/T 235-2014建筑反射隔热涂料
- 农机停放库棚可研报告效益分析
- 领带订做合同协议
- 研发工程师:新能源科技公司电池研发简历
- 高中家长会 高二期中家长会课件
- T-CPI 11037-2024 石油天然气钻采设备水力振荡器技术与应用规范
评论
0/150
提交评论