Java implementation read the file name of all files under the folder (including subdirectories) _java

Source: Internet
Author: User

In the process of programming, often use of the file read and write operations. For example, find all the file names under a folder, and so on.

The following program shows a program that gets the absolute path of all files under a given folder. Can be used as a certain module, when needed directly.

Package src;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.OutputStreamWriter;

Import Java.util.Vector;
  public class FileList {private String dir_name=null;
  Private String List_name=null;
  Private bufferedwriter out = null;
  
  Vector<string> Ver=null;  public FileList (String dir_name,string list_name) throws ioexception{This.dir_name=dir_name;  folder address This.list_name=list_name;  Save file List file address Ver=new vector<string> (); Used as stack} public void GetList () throws exception{out = new BufferedWriter (New OutputStreamWriter  Ream (List_name, true));
    Writes to the specified file Ver.add (dir_name) in an append manner;  while (Ver.size () >0) {file[] files = new File (ver.get (0). toString ()). Listfiles ();
      
      Get all the files (folders) under the folder name Ver.remove (0);
      int len=files.length;
        for (int i=0;i<len;i++) {String tmp=files[i].getabsolutepath (); if (files[i).Isdirectory ())//If it is a directory, join the queue.
        For subsequent processing of VER.ADD (TMP);    else Out.write (tmp+ "\ r \ n");
      If it is a file, the file name is output directly to the specified file.
  } out.close ();
 }
}


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.