What is the Java 7 or Java 8 way to create a file if that does not exists?
Not sure what you want, but for instance:
try {
Files.createFile(thePath);
} catch (FileAlreadyExistsException ignored) {
}
And there are other solutions; for instance:
if (!Files.exists(thePath, LinkOption.NOFOLLOW_LINKS))
Files.createFile(thePath);
Note that unlike File
, these will throw exceptions if file creation fails! And relevant ones at that (for instance, AccessDeniedException
, ReadOnlyFileSystemException
, etc etc)
See here for more information. Also see why you should migrate to java.nio.file
, quickly.