⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 counter.java

📁 21天学通JAVA2(第三版)(专业参考版)
💻 JAVA
字号:
package counter;

import java.io.*;
import java.util.*;

public class Counter {
    private int count;
    private String filepath;

    public Counter(String inFilepath) {
        count = 0;
        filepath = inFilepath;
    }

    public int getCount() {
        try {
            File countFile = new File(filepath);
            FileReader file = new FileReader(countFile);
            BufferedReader buff = new BufferedReader(file);
            String current = buff.readLine();
            count = Integer.parseInt(current);
            buff.close();
        } catch (IOException e) {
            // do nothing
        } catch (NumberFormatException nfe) {
            // do nothing
        }
        return count;
    }

    public void setCount(int newCount) {
        count = newCount;
        try {
            File countFile = new File(filepath);
            FileWriter file = new FileWriter(countFile);
            BufferedWriter buff = new BufferedWriter(file);
            String output = "" + newCount;
            buff.write(output, 0, output.length());
            buff.close();
        } catch (IOException e) {
            // do nothing
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -