java 获取jspwriter_jsp中ReaderFile方法来读取txt文件 WriterFile方法写入记录

jsp中利用UseBen 中的ReaderFile方法来读取txt文件 WriterFile方法写入记录,调用control对象的方法操作文件实例源码,jsp javabean操作txt文件实例:

JSP_File.jsp源文件:

-//W3C//DTD HTML 4.01 Transitional//EN”>

My JSP ‘JSP_File.jsp’ starting page

String filePath = application.getRealPath(“/”)+”WEB-INF/control.txt”;

//调用control对象的ReaderFile方法来读取control.txt的信息

String count =control.ReaderFile(filePath);

//调用control对象的WriterFile方法把独具的记录数+1后写入文件control.txt中

control.WriterFile(filePath,count);

%>

第访问者

Control.java源码:

package com.cn.jspandfile;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.PrintWriter;

public class Control {

//保存文本的变量

private String currentRecord =null;

//BufferedReader对象,用于读取文件数据

private BufferedReader file;

//完整的文件路径对象

private String path;

public Control(){

}

//ReaderFile用来读取数据

public String ReaderFile(String filePath) throws FileNotFoundException{

path = filePath;

File file =new File(filePath);

if(!file.exists()){

//如果文件不存在,就创建一个文件

try {

file.createNewFile();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

//创建BufferReader对象

BufferedReader bufferedReader = new BufferedReader(new FileReader(path));

String str =null;

//读取一行数据,并保存到currentRecord变量中

try {

currentRecord = bufferedReader.readLine();

} catch (IOException e) {

// 处理错误

System.out.println(“读取数据错误!”);

}

//如果currentRecord变量为空

if(currentRecord ==null){

//文件为空

str = “1″;

}else{

//文件不为空

str = currentRecord;

}

return str;

}

public void WriterFile(String filePath,String count) throws FileNotFoundException{

path = filePath;

//把count转化为int类型并加上1

int writerStr = Integer.parseInt(count)+1;

try {

//创建PrintWriter对象,用于把数据写入文件中

PrintWriter printWriter = new PrintWriter(new PrintWriter(filePath));

printWriter.println(writerStr);

//清楚PrintWriter对象

printWriter.close();

} catch (IOException e) {

//处理错误

System.out.println(“文件写入错误”+e.getMessage());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值