深度优先与广度优先遍历文件
2012-10-22 15:05
309 查看
package agrisom;
import java.io.File;
import java.io.IOException;
import java.util.Stack;
import java.util.Queue;
import java.util.LinkedList;
public class ListFile {
public static void main(String[] args) throws IOException {
File file = new File("C://Program Files//Common Files//System");
DFSWithStack(file);
System.out.println("--------------------------------");
DFSWithStack2(file);
System.out.println("--------------------------------");
DFS(file);
System.out.println("--------------------------------");
BFSWithQueue(file);
}
/**
* 深度非递归遍历文件
* 缺点是对于同一层次总是先输出所有的文件(子节点),再进行子目录的深度遍历,不太符合顺序
*/
private static void DFSWithStack(File file) throws IOException {
Stack<File> stack = new Stack<File>();
stack.push(file);
File fileInStack = null;
while (!stack.isEmpty()) {
fileInStack = stack.pop();
System.out.println(fileInStack.getCanonicalPath());
File[] files = fileInStack.listFiles();
for (File eachFile : files) {
if (eachFile.isFile()) {
System.out.println(eachFile.getCanonicalPath());
} else {
stack.push(eachFile);
}
}
}
}
/**
* 深度非递归遍历文件
* 保证遍历顺序
*/
private static void DFSWithStack2(File file) throws IOException {
Stack<File> stack = new Stack<File>();
stack.push(file);
File fileInStack = null;
while (!stack.isEmpty()) {
fileInStack = stack.pop();
System.out.println(fileInStack.getCanonicalPath());
File[] files = fileInStack.listFiles();
for (int i=0;files!=null && i<files.length;i++) {
File eachFile=files[i];
if (eachFile.isFile()) {
System.out.println(eachFile.getCanonicalPath());
} else {
//只要访问到子目录,就需要将所有后续子节点入栈
for(int j=i;j<files.length;j++){
stack.push(files[j]);
}
break;
}
}
}
}
//深度递归遍历文件
private static void DFS(File file) throws IOException {
System.out.println(file.getCanonicalPath());
File[] files = file.listFiles();
for (File eachFile : files) {
if (eachFile.isFile()) {
System.out.println(eachFile.getCanonicalPath());
} else {
DFS(eachFile);
}
}
}
//广度非递归遍历文件
private static void BFSWithQueue(File file) throws IOException {
System.out.println(file.getCanonicalPath());
Queue<File> queue = new LinkedList<File>();
queue.offer(file);
File fileInQueue = null;
while (queue.size() > 0) {
fileInQueue = queue.poll();
File[] files = fileInQueue.listFiles();
for (File eachFile : files) {
if (eachFile.isFile()) {
System.out.println(eachFile.getCanonicalPath());
} else {
System.out.println(eachFile.getCanonicalPath());
queue.offer(eachFile);
}
}
}
}
}
import java.io.File;
import java.io.IOException;
import java.util.Stack;
import java.util.Queue;
import java.util.LinkedList;
public class ListFile {
public static void main(String[] args) throws IOException {
File file = new File("C://Program Files//Common Files//System");
DFSWithStack(file);
System.out.println("--------------------------------");
DFSWithStack2(file);
System.out.println("--------------------------------");
DFS(file);
System.out.println("--------------------------------");
BFSWithQueue(file);
}
/**
* 深度非递归遍历文件
* 缺点是对于同一层次总是先输出所有的文件(子节点),再进行子目录的深度遍历,不太符合顺序
*/
private static void DFSWithStack(File file) throws IOException {
Stack<File> stack = new Stack<File>();
stack.push(file);
File fileInStack = null;
while (!stack.isEmpty()) {
fileInStack = stack.pop();
System.out.println(fileInStack.getCanonicalPath());
File[] files = fileInStack.listFiles();
for (File eachFile : files) {
if (eachFile.isFile()) {
System.out.println(eachFile.getCanonicalPath());
} else {
stack.push(eachFile);
}
}
}
}
/**
* 深度非递归遍历文件
* 保证遍历顺序
*/
private static void DFSWithStack2(File file) throws IOException {
Stack<File> stack = new Stack<File>();
stack.push(file);
File fileInStack = null;
while (!stack.isEmpty()) {
fileInStack = stack.pop();
System.out.println(fileInStack.getCanonicalPath());
File[] files = fileInStack.listFiles();
for (int i=0;files!=null && i<files.length;i++) {
File eachFile=files[i];
if (eachFile.isFile()) {
System.out.println(eachFile.getCanonicalPath());
} else {
//只要访问到子目录,就需要将所有后续子节点入栈
for(int j=i;j<files.length;j++){
stack.push(files[j]);
}
break;
}
}
}
}
//深度递归遍历文件
private static void DFS(File file) throws IOException {
System.out.println(file.getCanonicalPath());
File[] files = file.listFiles();
for (File eachFile : files) {
if (eachFile.isFile()) {
System.out.println(eachFile.getCanonicalPath());
} else {
DFS(eachFile);
}
}
}
//广度非递归遍历文件
private static void BFSWithQueue(File file) throws IOException {
System.out.println(file.getCanonicalPath());
Queue<File> queue = new LinkedList<File>();
queue.offer(file);
File fileInQueue = null;
while (queue.size() > 0) {
fileInQueue = queue.poll();
File[] files = fileInQueue.listFiles();
for (File eachFile : files) {
if (eachFile.isFile()) {
System.out.println(eachFile.getCanonicalPath());
} else {
System.out.println(eachFile.getCanonicalPath());
queue.offer(eachFile);
}
}
}
}
}
相关文章推荐
- 用深度优先和广度优先遍历文件夹下符合条件的文件
- 图的深度和广度优先遍历
- 【算法篇】栈和队列专题之广度优先遍历和深度优先遍历
- 深度优先和广度优先遍历及其 Java 实现
- 二叉树的深度优先和广度优先遍历
- C语言以邻接矩阵为存储结构的图的构造以及广度优先,深度优先遍历
- 图的深度、广度优先遍历
- 个人模板 图的深度,广度优先遍历
- 图 邻接矩阵 深度优先遍历 广度优先遍历
- 百度面试题——简要说明树的深度优先、广度优先遍历算法,及非递归实现的特点
- Java图的遍历(深度优先遍历、广度优先遍历)
- 图(三)图的遍历(深度优先和广度优先)
- 简要说明树的深度优先、广度优先遍历算法,及非递归实现的特点
- 数据结构中用邻接矩阵方式储存图,广度优先,深度优先遍历
- [置顶] 图:图的邻接矩阵创建、深度优先遍历和广度优先遍历详解
- 存储结构与邻接矩阵,深度优先和广度优先遍历及Java实现
- 图的深度优先和广度优先遍历及其Java实现
- 二叉树遍历(前序、中序、后序、层次遍历、深度优先、广度优先)
- 图 深度优先遍历 广度优先遍历 非递归遍历 图解算法过程
- 树的深度优先与广度优先遍历