java 创建Reader_BufferedReader类的对象建立问题(JAVA语言)

全文如下:(运行到"file=newBufferedReader(newFileReader(path));"时从path即*txt文件中读'不出数据,总是出现文件路径错误)packagecom.cf.txthtml.Counter;importjava.io.*;publiccla...

全文如下:

(运行到"file = new BufferedReader(new FileReader(path));"时从path即*txt文件中读'不出数据,总是出现文件路径错误)

package com.cf.txthtml.Counter;

import java.io.*;

public class Counter extends Object {

private String currentRecord = null;//保存文本的变量

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

private String path;//文件完整路径名

public Counter() {

}

//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;

}

//WriteFile方法用来将数据counter+1后加入到文本文件filePath中

//以实现计数器增涨的功能

public void WriteFile(String filePath,String counter) throws

FileNotFoundException

{

path = filePath;

//将counter转化为int类型并加一

int Writestr = Integer.parseInt(counter)+1;

try {

//新建PrintWriter对象,用于加入数据到文件中

PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));

//用文本格式打印整数Writestr

pw.println(Writestr);

//清除PrintWriter对象

pw.close();

} catch(IOException e) {

//异常处理

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

}

}

}

String cont=counter.ReadFile("/lyfcount.txt");

counter.WriteFile("/lyfcount.txt",cont);

%>

您是第位访问者

以上是我的页面程序,"/lyfcount.txt"就是path的值.

file = new BufferedReader(new FileReader(path));中file读不出文lyfcount.txt中的数据提示文件路径错误,而且在WEB的当前文件夹里我已建了lyfcount.txt文件,里面只有一初始值0

展开

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值