Using Files as Persistent Flags

Using Files as Persistent Flags

In Java, static variables may be used as flags to maintain state between different instantiations of a class (see Tip: “Using Static Variables to Store State Across Instantiations”). However, the static construct only allows a program to maintain state while the class is loaded. If the class is reloaded, the state is lost. This means when the Java program is run again, the runtime state of the flag will be lost (because the class is reloaded between consequent executions of the same program).

A simple way to persist flags between consequent executions of the program is to create a file that represents the “true” state of the boolean flag. If the file exists, the flag is true; if not, the flag is false:

 1. public class FileFlag {2.   private static File file_ = new File("flag.txt");3. 4.   public static boolean isFileFlag () {5.     return (file_.exists());6.   }7. 8.   public static void setFileFlag (boolean fileFlag) {9.     if (fileFlag) {10.     try {11. 	    FileOutputStream fos = new FileOutputStream(file_);12.        System.out.println("fileFlag_ is now ON");13. 	  }14. 	  catch (IOException ioe) { ioe.printStackTrace() };15. 	}16.     else {17.         file_.delete();18.         System.out.println("fileFlag_ is now OFF");19.     }20.   }21. 22.   public static void main (String[] args) {23.     System.out.println("fileFlag_  = " + FileFlag.isFileFlag());24.     String newFlag = args[0];25.     if (newFlag.equals("ON"))26.       FileFlag.setFileFlag(true);27.     else28.       FileFlag.setFileFlag(false);29.   }30. }

The class defines one attribute, which is a File class constructed from the String “flag.txt”. Two methods that set the state of the flag are defined. The method isFileFlag() method on Lines 4-6 checks if the file flag.txt exists on your file system and returns a boolean value that represents whether the file exists. Thus the existence of the file is the persistent flag.

The method setFileFlag() sets the flag based on the input boolean value. If the value of the parameter is true, it creates the file flag.txt on your system by creating a FileOutputStream for the file (see Tip: “Creating Files Using FileOutputStream”). If the parameter is false, the file is deleted from the system.

The main routine for this class takes in one String argument that is used to specify the parameter to be passed to setFileFlag(). On Line 23, the current flag is printed out. Here are some results of running this program:

 $ java FileFlag ONfileFlag = falsefileFlag_ is now ON$java fileFlag OFFfileFlag=truefileFlag_ is now OFF$java fileFlag OFFfileFlag=falsefileFlag_ is now OFF

As you can see, the last state of the flag is retained between consequent executions of the program. If you check your directory after turning the flag ON, you will see a zero-size file flag.txt there.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist