Java Delete Temp File deleteTempFiles()

Here you can find the source of deleteTempFiles()

Description

delete Temp Files

License

Open Source License

Declaration

public static void deleteTempFiles() throws IOException 

Method Source Code

//package com.java2s;

import java.io.File;

import java.io.FilenameFilter;
import java.io.IOException;

public class Main {
    public static void deleteTempFiles() throws IOException {
        File file = createTempFile("test", "test");
        String folder = getDirectoryForFile(file.getAbsolutePath());
        String[] list = new File(folder).list(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.startsWith("ohfu-");
            }//from  www .  j  a va 2  s .c  o m
        });
        if (list != null) {
            for (String n : list) {
                new File(path(folder, n)).delete();
            }
        }
    }

    public static File createTempFile(String prefix, String suffix) throws IOException {
        // this allows use to eaily identify all our dtemp files and delete them, since delete on Exit doesn't really work.
        File file = File.createTempFile("ohfu-" + prefix, suffix);
        file.deleteOnExit();
        return file;
    }

    public static String getDirectoryForFile(String filepath) {
        File f = new File(filepath);
        return f.getParent();
    }

    public static String path(String... args) {
        StringBuilder s = new StringBuilder();
        boolean d = false;
        for (String arg : args) {
            if (!d)
                d = !noString(arg);
            else if (!s.toString().endsWith(File.separator))
                s.append(File.separator);
            String a = arg;
            a = a.replace("\\", File.separator);
            if (s.length() > 0 && a.startsWith(File.separator))
                a = a.substring(File.separator.length());

            if ("..".equals(a)) {
                int i = s.substring(0, s.length() - 1).lastIndexOf(File.separator);
                s = new StringBuilder(s.substring(0, i + 1));
            } else
                s.append(a);
        }
        return s.toString();
    }

    public static boolean noString(String v) {
        return v == null || v.equals("");
    }

    public static boolean equals(String one, String two) {
        if (one == null && two == null)
            return true;
        if (one == null || two == null)
            return false;
        return one.equals(two);
    }
}

Related

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