'I have created a file and after some operation trying to delete it using file.delete(), but it is not working

I have created a file and after some operation trying to delete it using file.delete(), but it is not working. Below is my code.

public boolean isSafe(MultipartFile file) {
    boolean safeState = false;
    try {
        File f = new File("temp.pdf");
        if (!f.exists()) {
            f.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(file.getBytes());
        fos.flush();
        if ((f != null) && f.exists()) {
            PdfReader reader = new PdfReader(f.getAbsolutePath());
            String jsCode = reader.getJavaScript();
            if (jsCode == null) {
                PdfDictionary root = reader.getCatalog();
                PdfDictionary names = root.getAsDict(PdfName.NAMES);            
                PdfArray namesArray = null;
                if (names != null) {
                    PdfDictionary embeddedFiles = names.getAsDict(PdfName.EMBEDDEDFILES);
                    namesArray = embeddedFiles.getAsArray(PdfName.NAMES);
                }
                safeState = ((namesArray == null) || namesArray.isEmpty());
            }               
            fos.close();                
            reader.close();
            boolean flag = f.delete();
        }
    } catch (Exception e) {
        safeState = false;
    }
    return safeState;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source