Pytanie tylko wspomina aktualizację datownika, ale myślałem, że to umieścić tu tak czy inaczej. Szukałem dotyku jak w systemie Unix, który również utworzy plik, jeśli nie istnieje.
Dla każdego, kto używa Apache Commons, jest to właśnie FileUtils.touch(File file)
.
Oto source z (inlined openInputStream(File f)
):
public static void touch(final File file) throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file + "' exists but is a directory");
}
if (file.canWrite() == false) {
throw new IOException("File '" + file + "' cannot be written to");
}
} else {
final File parent = file.getParentFile();
if (parent != null) {
if (!parent.mkdirs() && !parent.isDirectory()) {
throw new IOException("Directory '" + parent + "' could not be created");
}
}
final OutputStream out = new FileOutputStream(file);
IOUtils.closeQuietly(out);
}
final boolean success = file.setLastModified(System.currentTimeMillis());
if (!success) {
throw new IOException("Unable to set the last modification time for " + file);
}
}
Ktoś powinien złożyć ten wniosek jako akcesorium do unix4j: https://github.com/tools4j/unix4j –
I nie rozumieją zależność między tytułem a pytaniem tutaj? – Lealo