In the process of programming, the read -write operation of files is often used. For example, find out all file names under a folder.
The following program is given to get the absolute path of all files under a given folder. Can be used as a module directly when needed.
package src; Import Java.io.BufferedWriter; Import Java.File; Import Java.io.fileoutStream; Import java.io.ioException; Import Java.o. utputstreamWriter; Import Java.util.vector; Public Class FileList {Private String dir_name = null; private string list_name = null; private bufferedwriter out = null; vector <string> Ver = NULLLIST (string Dir_nam e, string list_name) Throws IOEXception {this.dir_name = dir_name; // Folder address this. list_name = list_name; // Save the file address of the file list ver = New Vector <string> (); // Use it as a stack} Public void GetList () Throws Exception {Out = New Bufferedwriter (NEW O UTPUTSTREAMWRITER (New FileoutPutstream (List_name, TRUE ))); // Write into the specified file ver.add (dir_name); about (VER.Size ()> 0) {Files = New File (VER.GET (0) .t " ()). Listfiles (); // Get all the file (folder) ver.remove (0); int len = files.length; for (int i = 0; I <len; i ++) { String tmp = files [i] .getabsolutepath (); if (files [i] .sdirectory ()) // If it is a directory, add the queue. For follow -up processing Ver.add (TMP); else out.write (tmp+"/r/n"); // If it is a file, the file name is directly output to the specified file. }} out.close ();}}