Domanda

Voglio cambiare il timestamp di modifica di un file binario. Qual è il modo migliore per farlo?

Sarebbe una buona opzione aprire e chiudere il file? (Ho bisogno di una soluzione in cui la modifica del timestamp verrà modificata su ogni piattaforma e JVM).

È stato utile?

Soluzione

La classe File ha un metodo setLastModified . Questo è ciò che fa ANT.

Altri suggerimenti

I miei 2 centesimi, basati su @ Joe.M answer

public static void touch(File file) throws IOException{
    long timestamp = System.currentTimeMillis();
    touch(file, timestamp);
}

public static void touch(File file, long timestamp) throws IOException{
    if (!file.exists()) {
       new FileOutputStream(file).close();
    }

    file.setLastModified(timestamp);
}

Ecco un semplice frammento:

void touch(File file, long timestamp)
{
    try
    {
        if (!file.exists())
            new FileOutputStream(file).close();
        file.setLastModified(timestamp);
    }
    catch (IOException e)
    {
    }
}

So che Apache Ant ha un Task che fa proprio questo.
Vedi il codice sorgente di Touch (che può mostrarti come lo fanno)

Usano FILE_UTILS .setFileLastModified (file, modTime); , che utilizza ResourceUtils.setLastModified (nuovo FileResource (file), ora); , che utilizza un org.apache.tools.ant.types.resources.Touchable , implementato da < codice> org.apache.tools.ant.types.resources.FileResource ...

In pratica, si tratta di una chiamata a File.setLastModified (modTime) .

Questa domanda menziona solo l'aggiornamento del timestamp, ma ho pensato di metterlo comunque qui. Stavo cercando un tocco come in Unix che creerà anche un file se non esiste.

Per chiunque usi Apache Commons, c'è FileUtils.touch (File file) che fa proprio questo.

Ecco il source da ( in linea 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);
    }
}

Se stai già utilizzando Guava :

com.google.common.io.Files.touch (file)

Poiché File è una cattiva astrazione , è meglio usare Files e Path :

public static void touch(final Path path) throws IOException {
    Objects.requireNotNull(path, "path is null");
    if (Files.exists(path)) {
        Files.setLastModifiedTime(path, FileTime.from(Instant.now()));
    } else {
        Files.createFile(path);
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top