删除windows系统文件时,出现下面问题:
文件名对目标文件夹可能过长。您可以缩短文件名并重试,或者尝试路径较短的位置。
的解决办法。
package com.iteye.injavawetrust.ad; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; /** * 删除windows系统文件时,出现下面问题:<br> * 文件名对目标文件夹可能过长。您可以缩短文件名并重试,或者尝试路径较短的位置。<br> * 的java解决办法。 * @author InJavaWeTrust * */ public class DelFile { public static void main(String[] args) { try { String yourPath = "E:\InJavaWeTrust\miner\html\20160325"; String cmd = "cmd /C dir /x " + yourPath; Process process = Runtime.getRuntime().exec(cmd); InputStream is = process.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String tmp = ""; while ((tmp = br.readLine()) != null) { sb.append(tmp); } String ret = sb.toString(); String[] b = ret.split("~"); String name = ""; String ext = ""; String name2 = ""; String ext2 = ""; List<String> list = new ArrayList<String>(); for(int i = 0; i < b.length; i++){ if(i % 2 == 0){ name = b[i].substring(b[i].lastIndexOf(" ") + 1); if(b.length > 2 && i > 1){ ext2 = b[i].substring(0, b[i].indexOf(" ")); list.add(name2 + "~" + ext2); } }else{ ext = b[i].substring(0, b[i].indexOf(" ")); list.add(name + "~" + ext); if(b.length > 2) { name2 = b[i].substring(b[i].lastIndexOf(" ") + 1); } } } //del for(String l : list){ System.out.println(l); String delName = l; String delCmd = "cmd /C del " + yourPath + File.separator + delName; Runtime.getRuntime().exec(delCmd); } } catch (IOException e) { e.printStackTrace(); } } }