Here you can find the source of delFile(String filePathAndName)
public static void delFile(String filePathAndName)
//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; } } }