前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >java删除文件内容_java 删除文件中的数据[通俗易懂]

java删除文件内容_java 删除文件中的数据[通俗易懂]

作者头像
全栈程序员站长
发布2022-07-01 13:39:05
发布2022-07-01 13:39:05
4.7K0
举报

大家好,又见面了,我是你们的朋友全栈君。

如果只是想要文件中的内容,可以使用如下代码:FileOutputStream fs = new FileOutputStream(new File(“C:\\buyterms.txt”));

2、如果是想要文件夹中的内容,可以使用如下代码:package com.xx;

import java.io.File;

public class Test {

public static void main(String[] args) {

String fileRoot = “C:/Users/xx/Desktop/xx/xxx”;

delFolder(fileRoot);

System.out.println(“deleted”);

}

// // 删除完文件后删除文件夹

// // param folderPath 文件夹完整绝对路径

public static void delFolder(String folderPath) {

try {

delAllFile(folderPath); // 删除完里面所有内容

//不想删除文佳夹隐藏下面

// String filePath = folderPath;

// filePath = filePath.toString();

// java.io.File myFilePath = new java.io.File(filePath);

// myFilePath.delete(); // 删除空文件夹

} catch (Exception e) {

e.printStackTrace();

}

}

// 删除指定文件夹下所有文件

// param path 文件夹完整绝对路径

public static boolean delAllFile(String path) {

boolean flag = false;

File file = new File(path);

if (!file.exists()) {

return flag;

}

if (!file.isDirectory()) {

return flag;

}

String[] tempList = file.list();

File temp = null;

for (int i = 0; i

if (path.endsWith(File.separator)) {

temp = new File(path + tempList[i]);

} else {

temp = new File(path + File.separator + tempList[i]);

}

if (temp.isFile()) {

temp.delete();

}

if (temp.isDirectory()) {

delAllFile(path + “/” + tempList[i]);// 先删除文件夹里面的文件

// delFolder(path + “/” + tempList[i]);// 再删除空文件夹

flag = true;

}

}

return flag;

}

}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/131278.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档