Home > Java > javaTutorial > body text

Code example of how to read files recursively in Java

黄舟
Release: 2017-05-21 10:26:48
Original
1567 people have browsed it

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);
      }
    }
  }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template