大厂算法每日总结(统计文件夹下的文件)

//统计文件夹下的文件,是文件就累计1,隐藏文件空累计,文件不累计
public static void main(String[]args) {
System.out.println(getFileNumber("D:\重要文件"));
}
public static int getFileNumber(String folderPath) {
File root = new File(folderPath);
if(!root.isDirectory()&&!root.isFile()) {
return 0;
}
if (root.isFile()) {
return 1;
}
Stack stack= new Stack<>();
stack.add(root);
int files =0;
while(!stack.isEmpty()) {
File folder = stack.pop();
for(File next : folder.listFiles()) {
if(next.isFile()) {
files++;
}
if(next.isDirectory()) {
stack.push(next);
}
}
}
return files;
}

posted @   开源遗迹  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
点击右上角即可分享
微信分享提示