2

Here is my code of deleting the pdf file

try {
    File file = new File(docObjectId + ".pdf");
    file.setWritable(true);
    System.out.println(file.length());
    if (file.delete()) {
        System.out.println(file.getName() + " is deleted!");
    } else {
        System.out.println("Delete operation is failed.");
    }
} catch (Exception e) {
    e.printStackTrace();
}

It goes to the else part of the code.

PDF file is in project root folder and I am able to delete it manually. Scratching my head now.

Here is complete method. It might be due to some other reason

public Response getContractDocument(@PathParam("docid") String docObjectId) throws Exception {
    DocumentumService documentumService = new DocumentumService(documentumConfigUtil);
    DocumentumDocumentBean docDocumentBean = documentumService.getContractDocContent(docObjectId, true);

    FileInputStream fileInputStream;
    fileInputStream = new FileInputStream(docDocumentBean.getDocFile());
    compressPdf(fileInputStream,docObjectId + ".pdf");

    fileInputStream = new FileInputStream(docObjectId + ".pdf");


    ResponseBuilder responseBuilder = Response.ok((Object) fileInputStream);
    try {
        File file = new File(docObjectId + ".pdf");
        System.out.println(file.getAbsolutePath());
        file.setWritable(true);
        System.out.println(file.length());

        File d = new File(file.getAbsolutePath());
        if (d.delete()) {
            System.out.println(file.getName() + " is deleted!");
        } else {
            System.out.println("Delete operation is failed.");
        }
    } catch(Exception e) {
        e.printStackTrace();
    }
    return responseBuilder.build();
}
Zabuzard
  • 23,461
  • 7
  • 54
  • 77
Adeel
  • 383
  • 1
  • 5
  • 20
  • You're not deleting the file. You're if statement is just checking if the 'file' is deleted. – Jixone Aug 17 '17 at 01:45
  • The first thing which pops into my head is are you sure that the file path you used is correct? If relative, I'd check to make sure it is what you think it is. – Tim Biegeleisen Aug 17 '17 at 01:45
  • Try this, if (file.exists()) { file.delete(); } – Tehmina Aug 17 '17 at 01:48
  • sorry nothing worked .. I edit my question – Adeel Aug 17 '17 at 01:57
  • As you said that nothing has worked so far, are you able to delete other files with your code? Try to enclose the problem by detecting which part of your code fails. – Zabuzard Aug 17 '17 at 03:57

3 Answers3

6

My experience is with windows. The reason that a file won't delete is always the same. Some object has a connection to the file and is holding it open. In this case, it looks like it might be fileInputStream.

Try this before you attempt to delete:

fileInputStream.close();
Charles Knell
  • 322
  • 1
  • 6
  • yes you are right. I tried closing it but then in response I get nothing. I think I am lost in pointers and connections. – Adeel Aug 17 '17 at 04:24
  • Do you mean that the value of d.delete() is neither true or false? What did you mean by "in response I get nothing" ? – Charles Knell Aug 17 '17 at 04:34
  • return responseBuilder.build(); returns nothing but file get deleted successfully. – Adeel Aug 17 '17 at 04:35
  • I think that it's just a matter of reordering the code so that the fileInputStream.close() and file delete occurs after responseBuilder.build() is computed. – Charles Knell Aug 17 '17 at 05:01
0

Change if(file.delete) to

try {
    file.delete();
    System.out.println("file deleted");
} catch(IOException e) {
    System.out.println("file not deleted");
}

The exception may not be accurate.

Zabuzard
  • 23,461
  • 7
  • 54
  • 77
AnthonyK
  • 473
  • 2
  • 11
0

First, check if the file exist or not and then delete it.

Kindly use the below code. Its working fine and is very clear approach for deletion. I hope it would help.

public static void main(String[] args) {
    try{
        File file = new File("C:/Users/Tehmina Yaseen/Documents/NetBeansProjects/FileDeletion/src/filedeletion/Myfile.pdf");

        if (file.exists()) {
            file.delete();
            System.out.println(file.getName() + " is deleted!");
        } else {
            System.out.println("Delete operation is failed.");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }   
}

Here is the output:

Output of the above code

Zabuzard
  • 23,461
  • 7
  • 54
  • 77
Tehmina
  • 205
  • 2
  • 8