I'm trying to implement the following operation in Java and am not sure how:
* write data (Data is defined in my package)
* to a file only if it does not exist, return success
boolean writeData(File f, Data d)
FileOutputStream fos = null;
fos = atomicCreateFile(f);
if (fos != null)
/* write data here */
fos.close(); // needs to be wrapped in an exception block
File.createNewFile() only creates a file if one doesn't already exist.
EDIT: Based on your new description of wanting to lock the file after it's created you can use the
java.nio.channels.FileLock object to lock the file. There isn't a one line create and lock though like you are hoping. Also, see this SO question.