Home  >  Article  >  Java  >  Java implements traversing all files in a directory (including subdirectories)

Java implements traversing all files in a directory (including subdirectories)

王林
王林forward
2019-11-30 13:27:406481browse

Java implements traversing all files in a directory (including subdirectories)

There are two ways to traverse all the files in a folder in java:

1. Recursive traversal

It is usually the first method that developers can think of.

The advantages of recursive traversal are:

It is relatively simple to implement, the amount of code is relatively small, and the execution efficiency is high.

The disadvantages are:

It consumes more memory and has higher hardware requirements.

Online video tutorial sharing: java online video

The specific algorithm is as follows:

// 递归遍历
 private void getDirectory(File file) {
  File flist[] = file.listFiles();
  if (flist == null || flist.length == 0) {
      return 0;
  }
  for (File f : flist) {
      if (f.isDirectory()) {
          //这里将列出所有的文件夹
          System.out.println("Dir==>" + f.getAbsolutePath()); 
          getDirectory(f);
      } else {
         //这里将列出所有的文件
          System.out.println("file==>" + f.getAbsolutePath());
      }
  }
}

2. Non-recursive traversal

Specific ideas:

When traversing a folder, if it is a folder, it is added to the linkedlist, if it is a file, it is listed. In this way, the files and folders in the directory have been traversed, and all the folders have been saved in the linkedlist, so what is left is to traverse the files in the folders in the linkedlist. The traversal method is the same as the above operation.

If it is a folder, add it to the linkedlist. Of course, every time you take out a folder from the list, you need to delete the folder from the list. Here, linkedlist.removeFirst() is used to read it. Take, which reads the first element of the list each time and removes it from the list. In this way, as long as the status of linkedlist is traversed to isEmty, the traversal is completed.

The specific algorithm is as follows:

/**
	 * 非递归遍历
	 * @param file
	 * @return
	 */
	public static LinkedList<File> GetDirectory(String path) {
		File file = new File(path);
		LinkedList<File> Dirlist = new LinkedList<File>(); // 保存待遍历文件夹的列表
		LinkedList<File> fileList = new LinkedList<File>();
		GetOneDir(file, Dirlist, fileList);// 调用遍历文件夹根目录文件的方法
		File tmp;
		while (!Dirlist.isEmpty()) {
			tmp = (File) Dirlist.removeFirst();
			// 从文件夹列表中删除第一个文件夹,并返回该文件夹赋给tmp变量
			// 遍历这个文件夹下的所有文件,并把
			GetOneDir(tmp, Dirlist, fileList);
 
		}
		return fileList;
	}
 
	// 遍历指定文件夹根目录下的文件
	private static void GetOneDir(File file, LinkedList<File> Dirlist,
			LinkedList<File> fileList) {
		// 每个文件夹遍历都会调用该方法
		File[] files = file.listFiles();
 
		if (files == null || files.length == 0) {
			return;
		}
		for (File f : files) {
			if (f.isDirectory()) {
				Dirlist.add(f);
			} else {
				// 这里列出当前文件夹根目录下的所有文件,并添加到fileList列表中
				fileList.add(f);
				// System.out.println("file==>" + f);
 
			}
		}
	}

Recommended related articles and tutorials: java introductory tutorial

The above is the detailed content of Java implements traversing all files in a directory (including subdirectories). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete