?? counter.java
字號(hào):
package Counter;
import java.io.*;
public class counter extends Object
{
private String currentRecord = null;
//保存文本的變量
private String returnStr;
private BufferedReader file;
//BufferedReader對(duì)象,用于讀取文件數(shù)據(jù)
private String path;
//表示文件完整路徑名
public counter() {
}
//ReadFile方法用來(lái)讀取文件filePath中的數(shù)據(jù),并返回這個(gè)數(shù)據(jù)
public String ReadFile(String filePath) throws FileNotFoundException
{
path = filePath;
//創(chuàng)建新的BufferedReader對(duì)象
file = new BufferedReader(new FileReader(path));
returnStr=null;
try
{
//讀取一行數(shù)據(jù)并保存到currentRecord變量中
currentRecord=file.readLine();
}catch (IOException e)
{//錯(cuò)誤處理
System.out.println("讀取數(shù)據(jù)錯(cuò)誤.");
}
if (currentRecord =="")
//如果文件為空
{returnStr="0";}
else
{//文件不為空
returnStr=currentRecord;
}
//返回讀取文件的數(shù)據(jù)
return returnStr;
}
//ReadFile方法用來(lái)將數(shù)據(jù)counter+1后寫入到文本文件filePath中
//以實(shí)現(xiàn)計(jì)數(shù)增長(zhǎng)的功能
public void WriteFile(String filePath,String counter) throws FileNotFoundException
{
path = filePath;
//將counter轉(zhuǎn)換為int類型并加一
int Writestr = Integer.parseInt(counter)+1;
try {
//創(chuàng)建PrintWriter對(duì)象,用于寫入數(shù)據(jù)到文件中
PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
//用文本格式打印整數(shù)Writestr
pw.println(Writestr);
//清除PrintWriter對(duì)象
pw.close();
}catch(IOException e) {
System.out.println("寫入文件錯(cuò)誤"+e.getMessage());
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -