📄 counter.java
字号:
package org.com.JavaBean;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class Counter {
//保存文本的位置
private String currentRecord = null;
//bufferedReader对象,用于读取数据
private BufferedReader file;
//文件完整的路径名
private String path;
public Counter()
{
}
//ReadFile方法用来存取filepate中的数据,并返回这个数据
public String readFile(String filePath)throws FileNotFoundException{
path = filePath;
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;
}
//ReadFile方法用来将数据counter+1后写入文本文件filePath
//实现计数增长功能
public void writeFile(String filePath,int counter)throws FileNotFoundException
{
path = filePath;
try
{
//创建PrintWriter对象,用于写数据到文件
PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
//用于文本格式打印counter
pw.println(counter);
//清除对象
pw.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -