• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

jsp+java类+servlet实现文件读取、写入的功能(一)

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 92次 | 进入软件测试论坛讨论

领测软件测试网

本文是根据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){}
//}
}

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网