Java File Path Delete delFile(String filePathAndName)

Here you can find the source of delFile(String filePathAndName)

Description

del File

License

Apache License

Declaration

public static void delFile(String filePathAndName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.IOException;

public class Main {

    public static void delFile(String filePathAndName) {
        try {// www.ja v a2s.  c  o m
            String filePath = filePathAndName;
            filePath = filePath.toString();
            java.io.File myDelFile = new java.io.File(filePath);
            myDelFile.delete();
        } catch (Exception e) {
            e.printStackTrace();

        }

    }

    public static int delete(String pathName) {
        return delete(new File(pathName));
    }

    public static int delete(File filename) {
        if (filename.isFile()) {
            return deleteFileByDoc(filename.getAbsolutePath());
        } else {
            return deleteDirByDoc(filename.getAbsolutePath());
        }
    }

    public static int deleteFileByDoc(String path) {
        path = path.replaceAll("/", "\\\\");
        path = path.replaceAll("\\\\\\\\", "\\\\");
        if (new File(path).exists()) {
            Runtime runtime = Runtime.getRuntime();
            try {
                runtime.exec("cmd /c del " + path);
                return 0;
            } catch (IOException e) {
                return 1;
            }
        } else {
            return 2;
        }
    }

    public static int deleteDirByDoc(String path) {
        path = path.replaceAll("/", "\\\\");
        path = path.replaceAll("\\\\\\\\", "\\\\");
        if (new File(path).exists()) {
            Runtime runtime = Runtime.getRuntime();
            try {
                runtime.exec("cmd /c rd /s/q " + path);
                return 0;
            } catch (IOException e) {
                return 1;
            }
        } else {
            return 2;
        }
    }
}

Related

  1. deleteTempFile(String sourceFilePath)
  2. deleteTree(final File path)
  3. deleteWallpaper(String file_name, Map dimmensions, String basepath, List resolution_directories)
  4. delFile(String filePath)
  5. delFile(String filePath, String fileName)
  6. delFile(String filePathAndName)
  7. delFile(String filePathAndName)
  8. delFile(String filePathAndName)
  9. DelFile(String in_Path, ArrayList arrFileList)