Here you can find the source of safeDeleteFile(final String fileName)
public static void safeDeleteFile(final String fileName)
//package com.java2s; //License from project: Apache License import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; public class Main { public static void safeDeleteFile(final String fileName) { final FileSystem fs = FileSystems.getDefault(); final Path pdfFilePath = fs.getPath(fileName); try {/*from ww w. j a va2 s.c o m*/ Files.delete(pdfFilePath); } catch (Exception ex) { // do nothing } finally { // we actually want to keep it open // it might be expensive to keep open/close // and sometimes this throws exceptions // fs.close(); } } }