Java Delete File deleteFile(String fileName)

Here you can find the source of deleteFile(String fileName)

Description

delete File

License

Open Source License

Declaration

public static boolean deleteFile(String fileName) 

Method Source Code

//package com.java2s;

import java.io.File;

public class Main {

    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        if (file.exists() && file.isFile()) {
            return file.delete();
        }/*  w  w  w  .ja v  a 2 s . c  om*/
        return false;
    }

    public static boolean delete(String fileName) {
        File file = new File(fileName);
        if (!file.exists()) {
            return false;
        } else {
            if (file.isFile()) {
                return deleteFile(fileName);
            } else {
                return deleteDirectory(fileName);
            }
        }
    }

    public static boolean deleteDirectory(String dir) {
        if (!dir.endsWith(File.separator)) {
            dir = dir + File.separator;
        }
        File dirFile = new File(dir);
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return false;
        }
        boolean flag = true;
        File[] files = dirFile.listFiles();
        int filesLength = files.length;
        for (int i = 0; i < filesLength; i++) {
            if (files[i].isFile()) {
                flag = deleteFile(files[i].getAbsolutePath());
                if (!flag) {
                    break;
                }
            } else if (files[i].isDirectory()) {
                flag = deleteDirectory(files[i].getAbsolutePath());
                if (!flag) {
                    break;
                }
            }
        }
        if (!flag) {
            return false;
        }
        return dirFile.delete();
    }
}

Related

  1. deleteFile(String file)
  2. deleteFile(String file)
  3. deleteFile(String file)
  4. deleteFile(String filename)
  5. deleteFile(String fileName)
  6. deleteFile(String fileName)
  7. deleteFile(String filename)
  8. deleteFile(String filename)
  9. deletefile(String fileName)