java 创建Reader_BufferedReader类的对象建立问题(JAVA语言)
全文如下:(運(yùn)行到"file=newBufferedReader(newFileReader(path));"時(shí)從path即*txt文件中讀'不出數(shù)據(jù),總是出現(xiàn)文件路徑錯(cuò)誤)packagecom.cf.txthtml.Counter;importjava.io.*;publiccla...
全文如下:
(運(yùn)行到"file = new BufferedReader(new FileReader(path));"時(shí)從path即*txt文件中讀'不出數(shù)據(jù),總是出現(xiàn)文件路徑錯(cuò)誤)
package com.cf.txthtml.Counter;
import java.io.*;
public class Counter extends Object {
private String currentRecord = null;//保存文本的變量
private BufferedReader file; //BufferedReader對象,用于讀取文件數(shù)據(jù)
private String path;//文件完整路徑名
public Counter() {
}
//ReadFile方法用來讀取文件filePath中的數(shù)據(jù),并返回這個(gè)數(shù)據(jù)
public String ReadFile(String filePath) throws FileNotFoundException
{
path = filePath;
//構(gòu)建新的BufferedReader對象
file = new BufferedReader(new FileReader(path));
String returnStr =null;
try
{
//讀取一行數(shù)據(jù)并保存到currentRecord變量中
currentRecord = file.readLine();
}
catch (IOException e)
{//異常處理
System.out.println("讀取數(shù)據(jù)錯(cuò)誤.");
}
if (currentRecord == null)
//如果文件為空
returnStr = "沒有任何記錄";
else
{//文件不為空
returnStr =currentRecord;
}
//返回讀取文件的數(shù)據(jù)
return returnStr;
}
//WriteFile方法用來將數(shù)據(jù)counter+1后加入到文本文件filePath中
//以實(shí)現(xiàn)計(jì)數(shù)器增漲的功能
public void WriteFile(String filePath,String counter) throws
FileNotFoundException
{
path = filePath;
//將counter轉(zhuǎn)化為int類型并加一
int Writestr = Integer.parseInt(counter)+1;
try {
//新建PrintWriter對象,用于加入數(shù)據(jù)到文件中
PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
//用文本格式打印整數(shù)Writestr
pw.println(Writestr);
//清除PrintWriter對象
pw.close();
} catch(IOException e) {
//異常處理
System.out.println("寫入文件錯(cuò)誤"+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中的數(shù)據(jù)提示文件路徑錯(cuò)誤,而且在WEB的當(dāng)前文件夾里我已建了lyfcount.txt文件,里面只有一初始值0
展開
總結(jié)
以上是生活随笔為你收集整理的java 创建Reader_BufferedReader类的对象建立问题(JAVA语言)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1038:苹果和虫子
- 下一篇: https安全认证流程简介