Java Delete Temp File deleteTempFiles()

Here you can find the source of deleteTempFiles()

Description

delete Temp Files

License

Apache License

Declaration

public static void deleteTempFiles() throws IOException 

Method Source Code

//package com.java2s;
/*//from  ww w .  ja v a  2 s.  c  om
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;
import java.util.Collection;

import java.util.UUID;

public class Main {
    private static final File TMP = new File(System.getProperty("java.io.tmpdir"),
            "aether-" + UUID.randomUUID().toString().substring(0, 8));

    public static void deleteTempFiles() throws IOException {
        deleteFile(TMP);
    }

    public static void deleteFile(File file) throws IOException {
        if (file == null) {
            return;
        }

        Collection<File> undeletables = new ArrayList<File>();

        delete(file, undeletables);

        if (!undeletables.isEmpty()) {
            throw new IOException("Failed to delete " + undeletables);
        }
    }

    private static void delete(File file, Collection<File> undeletables) {
        String[] children = file.list();
        if (children != null) {
            for (String child : children) {
                delete(new File(file, child), undeletables);
            }
        }

        if (!del(file)) {
            undeletables.add(file.getAbsoluteFile());
        }
    }

    private static boolean del(File file) {
        for (int i = 0; i < 10; i++) {
            if (file.delete() || !file.exists()) {
                return true;
            }
        }
        return false;
    }
}

Related

  1. deleteTempFile()
  2. deleteTempFile(File f)
  3. deleteTempFile(final File tempFile)
  4. deleteTempFile(String fileId)
  5. deleteTempFiles()
  6. deleteTempFiles()
  7. deleteTempFiles()
  8. deleteTempFiles(final File workFolder)
  9. deleteTempFiles(Map fileMap, String templateFileName)