0
点赞
收藏
分享

微信扫一扫

某2015年写的仿window tree命令遍历打印


项目源码​​https://gitee.com/51bwn/JavaProject​​

**
* 目录递归
*/
public void dirRecurrencePrint(String filePath) {
File f = new File(filePath);
if (f != null) {
File[] files = f.listFiles();
for (File file : files) {
if (file.isFile()) {
System.out.println("文件:" + file.getPath());
} else {
System.out.println("目录:" + file.getPath());
dirRecurrencePrint(file.getPath());

}

}
}

}
/**
* 高仿windows 的tree 包括了文件夹 和文件生成└─-结尾
* @param filePath
* @param tab
*/
public void dirRecurrencePrint1(String filePath, String tab) {

File f = new File(filePath);
File[] fileDirs = f.listFiles();//列出所有文件和目录
String temp="│";
if(fileDirs==null){
return;
}
for (File file : fileDirs) {

//发现这里写 文件过滤器多此一举而且竟然无效 为什么多此一举 因为 文件目录在fileDirs里面的最后面的
if (file.isFile()) {
if(file==fileDirs[fileDirs.length-1])
{ temp="";
System.out.println(tab + "└─-" + file.getName());

}
else
{
System.out.println(tab + "├─-" + file.getName());
}

} else {
File[] dirs = f.listFiles(new FilterDir());//先取出所有文件夹然后跟最后一个文件夹判断
if(file==dirs[dirs.length-1])//此方法 在只有文件夹的时候有效
{
// tab=tab.replace("│", "");
//System.out.println("---------------------------");
temp="";
System.out.println(tab + "└─-" + file.getName());
}
else
{
System.out.println(tab + "├─-" + file.getName());

}

dirRecurrencePrint1(file.getPath(), tab + temp+" ");
}


}
/*
* ├─Com│ └─Ac└ ─ca
*/

}
public void dirRecurrencePrint(String filePath, String tab) {
File f = new File(filePath);
String temp="│";
File[] files = f.listFiles(new FilterDir());
for (File file : files) {
if (file.isFile()) {
/* if(file==files[files.length-1])
{
System.out.println(tab + "└─-" + file.getName());
}
else
{
System.out.println(tab + "├─-" + file.getName());
}*/

} else {
//tab=tab.replaceAll("│", " ");
if(file==files[files.length-1])//此方法 在只有文件夹的时候有效
{
//tab=tab.replaceAll("│", " ");
temp="";
System.out.println(tab + "└─-" + file.getName());//是结尾就删除 分割线标记

//tab=tab.replaceAll("\\\\│", " ");

// System.out.println("tab:"+tab);
}
else
{
System.out.println(tab + "├─-" + file.getName());
}

dirRecurrencePrint(file.getPath(),tab + temp+" ");
}


}
/*
* ├─Com│ └─Ac└ ─ca
*/

}

public void dirRecurrencePrint(File file) {
File[] files = file.listFiles();
for (File file_temp : files) {
if (file_temp.isFile()) {
System.out.println("文件:" + file_temp.getPath());
} else {
// System.out.println(file_temp.listf;
System.out.println("目录:" + file_temp.getPath());
dirRecurrencePrint(file_temp);
}

}

}

/**
* 删除 目下 子目录所有文件
*
* @param filePath
*/
public void dirRecurrenceDelFile(String filePath) {
File f = new File(filePath);
File[] files = f.listFiles();
for (File file : files) {
if (file.isFile()) {
System.out.println("已删除文件:" + file.getPath());
file.delete();
} else {
System.out.println("进入目录:" + file.getPath());
dirRecurrenceDelFile(file.getPath());

}

}

}

/**
* 删除目录下所有文件 和文件夹 不包含此目录本身
*
* @param filePath
*/
public void dirRecurrenceDel(String filePath) {
File f = new File(filePath);
File[] files = f.listFiles();
for (File file : files) {
if (file.isFile()) {
if (file.delete()) {
System.out.println("已删除文件:" + file.getPath());
} else {
System.out.println("删除文件失败:" + file.getPath());
}
}

else {
System.out.println("进入目录:" + file.getPath());
dirRecurrenceDelFile(file.getPath());

}
// 循环到最底层之后 删除



}
//此方法防止到for循环里将不会删除根文件夹
if (f.delete()) {
System.out.println("已删除目录:" + f.getPath());
} else {
System.out.println("删除目录失败:" + f.getPath());
}

}
}

window的结构是这样的

│  .gitignore
│ applib.iml
│ build.gradle
│ proguard-rules.pro

├─build
│ ├─generated
│ │ │ mockable-android-26.v3.jar
│ │ │
│ │ ├─assets
│ │ │ └─shaders
│ │ │ ├─debug
│ │ │ └─release
│ │ ├─res
│ │ │ ├─pngs
│ │ │ │ ├─androidTest
│ │ │ │ │ └─debug
│ │ │ │ ├─debug
│ │ │ │ └─release
│ │ │ ├─resValues
│ │ │ │ ├─androidTest
│ │ │ │ │ └─debug
│ │ │ │ ├─debug
│ │ │ │ └─release
│ │ │ └─rs
│ │ │ ├─androidTest
│ │ │ │ └─debug
│ │ │ ├─debug
│ │ │ └─release
│ │ └─source
│ │ ├─aidl
│ │ │ ├─androidTest
│ │ │ │ └─debug
│ │ │ ├─debug
│ │ │ └─release
│ │ ├─apt
│ │ │ ├─debug
│ │ │ │ ├─android
│ │ │ │ │ └─databinding
│ │ │ │ │ DataBindingComponent.java
│ │ │ │ │
│ │ │ │ ├─cn
│ │ │ │ │ └─qssq666
│ │ │ │ │ └─rapiddevelopframe
│ │ │ │ │ │ BR.java
│ │ │ │ │ │
│ │ │ │ │ └─databinding
│ │ │ │ │ LayoutDataValuesBinding.java
│ │ │ │ │ RecyclerviewRefreshBinding.java
│ │ │ │ │ ViewDataEmptyBinding.java
│ │ │ │ │
│ │ │ │ └─com
│ │ │ │ └─android
│ │ │ │ └─databinding
│ │ │ │ └─library
│ │ │ │ └─baseAdapters
│ │ │ │ BR.java
│ │ │ │
│ │ │ └─release
│ │ │ ├─android
│ │ │ │ └─databinding
│ │ │ │ DataBindingComponent.java
│ │ │ │
│ │ │ ├─cn
│ │ │ │ └─qssq666
│ │ │ │ └─rapiddevelopframe
│ │ │ │ │ BR.java
│ │ │ │ │
│ │ │ │ └─databinding
│ │ │ │ LayoutDataValuesBinding.java
│ │ │ │ RecyclerviewRefreshBinding.java
│ │ │ │ ViewDataEmptyBinding.java
│ │ │ │
│ │ │ └─com
│ │ │ └─android
│ │ │ └─databinding
│ │ │ └─library
│ │ │ └─baseAdapters
│ │ │ BR.java
│ │ │
│ │ ├─buildConfig
│ │ │ ├─androidTest
│ │ │ │ └─debug
│ │ │ │ └─cn
│ │ │ │ └─qssq666
│ │ │ │ └─rapiddevelopframe
│ │ │ │ └─test
│ │ │ │ BuildConfig.java
│ │ │ │
│ │ │ ├─debug
│ │ │ │ └─cn
│ │ │ │ └─qssq666
│ │ │ │ └─rapiddevelopframe
│ │ │ │ BuildConfig.java
│ │ │ │
│ │ │ └─release
│ │ │ └─cn
│ │ │ └─qssq666
│ │ │ └─rapiddevelopframe
│ │ │ BuildConfig.java
│ │ │
│ │ ├─dataBinding
│ │ │ ├─baseClasses
│ │ │ │ ├─debug
│ │ │ │ └─release
│ │ │ └─trigger
│ │ │ ├─debug
│ │ │ │ └─android
│ │ │ │ └─databinding
│ │ │ │ └─layouts
│ │ │ │ DataBindingInfo.java
│ │ │ │
│ │ │ └─release
│ │ │ └─android
│ │ │ └─databinding
│ │ │ └─layouts
│ │ │ DataBindingInfo.java
│ │ │
│ │ ├─r
│ │ │ ├─androidTest
│ │ │ │ └─debug
│ │ │ │ ├─android
│ │ │ │ │ ├─arch
│ │ │ │ │ │ └─lifecycle
│ │ │ │ │ │ R.java
│ │ │ │ │ │
│ │ │ │ │ └─support
│ │ │ │ │ ├─compat
│ │ │ │ │ │ R.java
│ │ │ │ │ │
│ │ │ │ │ ├─coreui
│ │ │ │ │ │ R.java
│ │ │ │ │ │
│ │ │ │ │ ├─coreutils
│ │ │ │ │ │ R.java
│ │ │ │ │ │
│ │ │ │ │ ├─design
│ │ │ │ │ │ R.java
│ │ │ │ │ │
│ │ │ │ │ ├─fragment
│ │ │ │ │ │ R.java

举报

相关推荐

0 条评论