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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may