How to delete all temp files which created by createTempFile when exit an App in android?

前端 未结 7 2333
清酒与你
清酒与你 2021-02-07 15:06

I use the following code to create some temp files, and wrapped tem as inputsteam to send to client side.

I understand that the temp files can be deleted automatically b

7条回答
  •  长情又很酷
    2021-02-07 15:22

    It would be better if you can maintain a Set (Avoid Duplicates) for all files you create.

    And iterate the file list and delete every file one by one.

     public static final Set TMP_FILES = new HashSet<>();
    

    And delete all by iterating.

    public void deleteTempFiles() {
      for(String myTempFile: TMP_FILES) {
        try {
          new File(myTempFile).delete();
        } catch (Exception e) {
          // Handle if needed.
        }
      }
    }
    

提交回复
热议问题