Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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-");
            }
        });
        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) {
        int i = filepath.lastIndexOf(File.separator);
        if (i == -1)
            return filepath;
        else
            return filepath.substring(0, i);
    }

    public static String path(String... args) {
        StringBuilder s = new StringBuilder();
        boolean d = false;
        for (String arg : args) {
            if (!d)
                d = true;
            else if (!s.toString().endsWith(File.separator))
                s.append(File.separator);
            s.append(arg);
        }
        return s.toString();
    }
}