JAVA CERTIFICATION QUESTION: The Java FileChannel class
=>Using a file lock to ensure that only a single instance of an application is running
Imagine you are writing an application that must be restricted to running a single instance on a single machine.
To implement this requirement, you have decided to lock a file when the application starts.
If the lock attempt fails, the code generates a message and shuts down the new instance of the application.
You have decided to use the FileLock class obtained from the FileChannel class to implement this, as follows:
public class App { public static void main(String[] args) throws IOException { try (FileOutputStream fos = new FileOutputStream("/var/lock/app"); FileChannel channel = ... // missing code FileLock lock = channel.tryLock();) { if (lock == null) { System.out.println("Another instance is running, exiting..."); System.exit(0); } System.out.println("Started ..."); System.console().readLine("Click Enter to exit..."); } } }
How can you get an instance of the FileChannel to accomplish the requirement?
Assume the rest of the code is valid and the file exists on the file system. Choose one.
A. fos.getChannel();
B. new FileChannel(fos);
C. FileChannel.open(fos);
D. FileSystemProvider.newFileChannel(fos);
·Ɐ uoᴉʇdo sᴉ ɹǝʍsuɐ ʇɔǝɹɹoɔ ǝɥꓕ ·uoᴉsnๅɔuoꓛ