"Java" reads the path of all folders and files under it

Source: Internet
Author: User

If in the F:\aa folder, like the folder and file:


So, in Java, this reads all the file paths under F:/AA:

1, first because of the use of the file and the container class under the ArrayList, so at the beginning to introduce the following package:

Import Java.io.*;import java.util.*;

2, the method is as follows, where file Dirfile in addition to the letter, such as F:, can accept all the legal path. Because the drive letter contains some system files that are denied access, NULL pointer exceptions may occur because of reading the drive letter.

Here is simply querying all the folders, files under the current path and storing their paths to the file array//Because the folder does not query it contains all subfolders, files, so there is no need to use the recursive public static arraylist<string> Dir ( File dirfile) throws Exception {arraylist<string> Dirstrarr = new arraylist<string> (); if (Dirfile.exists ()) {//Direct removal take advantage of listfiles () to store all folders and files under the current path into an array of files files[] = Dirfile.listfiles (); for (File file:files) {// If the passed argument Dirfile is terminated with a file delimiter, that is,/or \, then the IF (Dirfile.getpath (). EndsWith (File.separator)) {Dirstrarr.add ( Dirfile.getpath () + file.getname ());} else {//otherwise, if there is no file delimiter, then a file delimiter is added, plus the filename is the path Dirstrarr.add (Dirfile.getpath () + file.separator+ file.getname ());}} return Dirstrarr;}
The method on it is not to read the xlsx under the new folder under the F:\aa, if in the process of reading, when you encounter a folder to read all of its subfolders and files simultaneously, use recursion to set a global dynamic array first:

public static arraylist<string> Dirallstrarr = new arraylist<string> ();
Then the method is as follows:

public static void Dirall (file dirfile) throws Exception {if (dirfile.exists ()) {File files[] = Dirfile.listfiles (); for (F Ile file:files) {//If a folder is encountered, it is called recursively. if (File.isdirectory ()) {//Recursive call Dirall (file);} else {//) If a folder is encountered, put the array if (Dirfile.getpath (). EndsWith (File.separator)) { Dirallstrarr.add (Dirfile.getpath () + file.getname ());} else {Dirallstrarr.add (Dirfile.getpath () + file.separator+ file.getname ());}}}}
In fact, in the process of reading, the key is to use the Listfiles () method, get all the file list under this folder, and "Java" move the folder and all its sub-files and subfolders (click the Open link), "Java" Use the file input and output stream to complete the operation of another folder where all files in one folder are copied (click the Open link), and the folder is recursive.

The whole method above comes up with a Java file like this:

Import Java.io.*;import java.util.*;p ublic class FileList {//Set a global dynamic array to hold the file path//main traversal folder, including all subfolders, files in case of recursion, So to set public static arraylist<string> Dirallstrarr = new arraylist<string> ();//This is just to query all folders under the current path, File and store its path to an array of files//because it encounters a folder that does not query its containing all subfolders, files, and therefore does not need to use recursive public static arraylist<string> Dir (file dirfile) throws Exception {arraylist<string> Dirstrarr = new arraylist<string> (), if (Dirfile.exists ()) {//Direct extraction using listfiles () Place all folders and files under the current path into an array of file files[] = Dirfile.listfiles (); for (File file:files) {//If the parameter passed Dirfile is a file delimiter, i.e./or \ end, then construct if (Dirfile.getpath (). EndsWith (File.separator)) {Dirstrarr.add (Dirfile.getpath () + file.getname ());} else { Otherwise, if there is no file delimiter, then a file delimiter is added, plus the filename, is the path Dirstrarr.add (Dirfile.getpath () + file.separator+ file.getname ());}} return Dirstrarr;} public static void Dirall (file dirfile) throws Exception {if (dirfile.exists ()) {File files[] = Dirfile.listfiles (); for (F Ile file:files) {//If a folder is encountered, it is called recursively. if (File.isdirectory ()) {//PassCall Dirall (file);} else {//If a folder is encountered, put the array if (Dirfile.getpath (). EndsWith (File.separator)) {Dirallstrarr.add (Dirfile.getpath () + File.getname ());} else {Dirallstrarr.add (Dirfile.getpath () + file.separator+ file.getname ());}}}} public static void Main (string[] args) throws Exception {file Dirfile = new File ("F:/aa"); System.out.println (Dir (dirfile));D Irall (dirfile); System.out.println (Dirallstrarr);}}

The results of the operation are as follows:


"Java" reads the path of all folders and files under it

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.