您的位置:首页 > 编程语言 > Java开发

Java删除文件夹 及目录下 文件 递归操作

2013-12-19 14:47 671 查看
public class FileOpeUtil {

public static void main(String[] args) throws IOException {

// delAllFiles("C://Users/13075012/.swt/");

String s = "D://opt/opt/ds.json";

/*

* String name1 = s.substring(0,s.lastIndexOf("/")+1); System.out.println(name1); String name =

* s.substring(s.lastIndexOf("/")+1); System.out.println(name); File f = new File(name1); if(!f.exists()){

* f.mkdirs(); }

*/

File f2 = new File(s);

System.out.println(f2.createNewFile());

}

public static void dealWithDir(String prefixPath, String lockerCode, boolean flag) {

delAllFiles(prefixPath);

if (flag) {

File file = new File(prefixPath + lockerCode);

file.mkdir();

}

}

private static void delAllFiles(String path) {

File file = new File(path);

if (!file.exists()) {

return;

}

if (!file.delete()) {

delAllFolders(file.getPath());

} else {

file.delete();

}

}

private static void delAllFolders(String path) {

File file = new File(path);

if (!file.exists()) {

return;

}

if (file.isDirectory()) {

File[] files = file.listFiles();

for (File f : files) {

delAllFiles(f.getPath());

delAllFolders(f.getPath());

f.delete();

}

}

if (file.isFile()) {

file.delete();

}

}

}

============================================

String absolutePath = request.getSession().getServletContext()

.getRealPath(File.separator + "software" + File.separator + "updatelist.json");

File jsonFile = new File(absolutePath);

if (!jsonFile.exists()) {

String absuluDir = absolutePath.substring(0, absolutePath.lastIndexOf(File.separator) + 1);

File dirFile = new File(absuluDir);

if (!dirFile.exists()) {

dirFile.mkdirs();

}

jsonFile.createNewFile();

}

br = new BufferedReader(new InputStreamReader(new FileInputStream(jsonFile)));

while ((line = br.readLine()) != null) {

stringBuffer.append(line);

}

JSONObject jsonObj = JsonFileUtil.dealWithUpdate(stringBuffer.toString(), lockerCode, delFlag,

orginFileName);

// 将json数据再次写入文件

bw = new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(new File(

absolutePath)))));

bw.write(jsonObj.toString());

bw.flush();// 强制将缓冲区 写入
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐