简体   繁体   English

计数程序执行的次数

[英]Count No of times Program has been executed

How can I get the number of times a program has previously run in java without keeping a file and tallying. 我如何在不保留文件和计数的情况下获取以前在Java中运行过的程序的次数。 Is there a Application class or something in java to check the count 是否有Application类或Java中的某些类来检查计数

You could use the preferences API . 您可以使用首选项API This eliminates the need for you to do the file i/o. 这消除了执行文件I / O的需要。 You will still need to read the preference value, increment it, and then store the new value. 您仍然需要读取首选项值,对其进行递增,然后存储新值。

On Windows, the preferences API uses the registry. 在Windows上,首选项API使用注册表。 On Unix, the preferences API uses the file system as the backing store (but this is hidden from you as a developer). 在Unix上,首选项API使用文件系统作为后备存储(但是对于开发人员来说,这是隐藏的)。

You can get/store preferences for either the system, or for an individual user. 您可以获取/存储系统或单个用户的首选项。 (It's not clear if you want to know how many times a user ran the program, or how many times anyone on said computer ran the program). (尚不清楚您是否想知道用户运行了该程序多少次,或者说所述计算机上的任何人运行了该程序多少次)。 Look for Preferences.systemNodeForPackage() and Preferences.userNodeForPackage() in the documentation . 文档中查找Preferences.systemNodeForPackage()Preferences.userNodeForPackage()

You can do something like: 您可以执行以下操作:

public class Test {

    private static int INSTANCE_COUNTER = 0;

    public Test() {

        INSTANCE_COUNTER++;
    }

    public static final int getInstanceCounter() {

        return INSTANCE_COUNTER;
    }

    //-Methods-//
}

Now, you can call Test.getInstanceCounter() to get the number. 现在,您可以调用Test.getInstanceCounter()来获取数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM