code: public static void writeToEncryptedFile(EncryptionKey encryptionKey, File file, byte[] data) throws IOException,
  	                                                                                                    NoSuchPaddingException,
  	                                                                                                    InvalidAlgorithmParameterException,
  	                                                                                                    NoSuchAlgorithmException,
  	                                                                                                    IllegalBlockSizeException,
  	                                                                                                    BadPaddingException,
  	                                                                                                    InvalidKeyException,
  	                                                                                                    EncryptionException {
  		AtomicFile atomicFile = new AtomicFile(file);
  		FileOutputStream stream = null;
  		boolean successful = false;
  		try {
  			stream = atomicFile.startWrite();
  			stream.write(encrypt(encryptionKey, data));
  			atomicFile.finishWrite(stream);
  			successful = true;
  		} finally {
  			if (!successful) {
  				atomicFile.failWrite(stream);
  			}
  		}
  	}
docs: region File IO