Fichier avec lock

PHOTO EMBED

Fri Jul 21 2023 17:46:46 GMT+0000 (Coordinated Universal Time)

Saved by @ambre

private void writeFile(String path, byte[] bytes) throws StorageException {
        String completePath = storage + path;
        File destination = new File(completePath);
        makeDirectories(destination);
        try (RandomAccessFile fileOnDisk = new RandomAccessFile(completePath, "rw"); //random signifie qu'on peut écrire où on veut dans le fichier
             FileChannel channel = fileOnDisk.getChannel()) {

            channel.lock();
            channel.truncate(0); //force une réécriture totale du fichier
            fileOnDisk.write(bytes);
        } catch (IOException e) {
            throw new StorageException(ERROR_SAVING_FILE, e.getMessage());
        }
    }
content_copyCOPY