Home  >  Article  >  Java  >  Code example of how to read files recursively in Java

Code example of how to read files recursively in Java

黄舟
黄舟Original
2017-05-21 10:26:481566browse

This article introduces you to the javarecursive method of reading files through a sample code. The code is simple and easy to understand, very good, and has reference value. Friends who need it can refer to it. Bar

Java recursively lists all files in a directory

/**
 * 列出指定目录的全部内容
 * */
import java.io.*;
class Recursion{
  public static void main(String[] args) {
    String fileName="D:"+File.separator;
    File f=new File(fileName);
    printFile(f);
  }
  public static void printFile(File f){
    if(f!=null){
      if(f.isDirectory()){
        File[] fileArray=f.listFiles();
        if(fileArray!=null){
          for (int i = 0; i < fileArray.length; i++) {
            //递归调用
            print(fileArray[i]);
          }
        }
      }
      else{
        System.out.println(f);
      }
    }
  }
}

The above is the detailed content of Code example of how to read files recursively in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn