import java.io.IOException;
import java.nio.file.AccessDeniedException;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) throws IOException {
        Path p = Path.of("c:\\4.png");
        try {
            unsetDosReadOnly(p);
            Files.delete(p);
        } catch(AccessDeniedException e) {
            unsetDosReadOnly(p);
            Files.delete(p);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    static void unsetDosReadOnly(Path p) throws IOException {
        var attrs = Files.getFileAttributeView(p, java.nio.file.attribute.DosFileAttributeView.class);
        if(attrs != null) {
            attrs.setReadOnly(false);
        }
    }
}