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 ();
}
}