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!