第20天-07-IO流(递归)

package bxd;

import java.io.File;

public class FileDemo3 {

    // 非递归打印
public static void showDir_1(File dir) { File[] files = dir.listFiles();
for (File file : files) {
System.out.println(file);
}
} // 递归打印
public static void showDir_2(File dir) { File[] files = dir.listFiles();
for (File file : files) {
System.out.println(file);
if (file.isDirectory())
showDir_2(file);
}
} // 逻辑更清晰的递归打印
public static void showDir_3(File dir) { System.out.println(dir); for (File file : dir.listFiles()) {
if (file.isDirectory()) {
showDir_3(file);
} else {
System.out.println(file);
}
}
} // 错误的递归, 相当于无限循环
public static void method() {
method();
} public static void toBin(int number, StringBuilder sb) {
if (number > 0) {
toBin(number / 2, sb);
sb.append(number % 2);
}
} public static int getSum(int number) {
if (number == 1) {
return 1;
}
return number + getSum(number - 1);
} public static void main(String[] args) { showDir_3(new File("D:/BaiduYunDownload/毕向东_Java基础视频教程")); StringBuilder stringBuilder = new StringBuilder();
toBin(6, stringBuilder);
System.out.println(stringBuilder); System.out.println(getSum(100));
}
}

第20天-08-IO流(带层次)

package bxd;

import java.io.File;

public class FileDemo4 {

    public static String getPrefix(int level) {
StringBuilder sb = new StringBuilder("|--");
for (int x = 1; x < level; x++) {
sb.insert(0, "| ");
}
return sb.toString();
} public static void showDir(File dir, int level) { level++;
for (File file : dir.listFiles()) {
System.out.println(getPrefix(level) + file);
if (file.isDirectory())
showDir(file, level);
}
} public static void main(String[] args) {
showDir(new File("D:/BaiduYunDownload/毕向东_Java基础视频教程"), 0);
}
}

第20天-09-IO流(删除带内容的目录)

package bxd;

import java.io.File;

/*
原理: 在Windows中删除目录需要从里往外删除. 删除过程可以用到递归.
*/
public class FileDemo5 {
public static void main(String[] args) {
removeDir(new File("D:/PS4 - Copy"));
} public static void removeDir(File dir) {
File[] files = dir.listFiles(); for (File file : files) {
if (file.isDirectory()) {
removeDir(file);
} else {
System.out.println(file.toString() + " -- " + file.delete());
}
}
System.out.println(dir + " --dir-- " + dir.delete());
}
}

第20天-10-IO流(创建.java文件列表)

package bxd;

/*
输出一个.java文件的扫描结果, 思路:
1. 对指定的目录进行递归
2. 获取递归过程中所有的.java文件
3. 将这些路径存储到集合中
4. 将集合中的数据写入到一个文件中
*/ import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; public class FileDemo6 {
public static void main(String[] args) throws IOException { File dir = new File("C:/Users/Eric/IdeaProjects/tomcat-jsp/");
List<File> list = new ArrayList<>(); fileToList(dir, list);
writeToFile(list, new File("list.txt"));
} public static void fileToList(File dir, List<File> list) {
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
fileToList(file, list);
} else {
if (file.getName().endsWith(".java"))
list.add(file);
}
}
} public static void writeToFile(List<File> list, File file) throws IOException {
BufferedWriter bufw = null;
try {
bufw = new BufferedWriter(new FileWriter(file));
for (File f : list) {
bufw.write(f.getAbsolutePath());
bufw.newLine();
bufw.flush();
}
} catch (IOException e) {
throw e;
} finally {
if (bufw != null) bufw.close();
}
}
}
05-11 17:22