Java fileutil
Import java. Io. file;
Import java. util. arraylist;
Public class fileutil {
/**
* Obtain the file list in the directory.
* @ Param strpath directory
* @ Return file list
*/
Public static arraylist getfilelist (string strpath ){
Arraylist filelist = NULL;
Return getfilelist (strpath, filelist );
}
Private Static arraylist getfilelist (string strpath, arraylist filelist ){
If (filelist = NULL ){
Filelist = new arraylist ();
}
File dir = new file (strpath );
File [] files = dir. listfiles ();
If (Files = NULL) return NULL;
For (INT I = 0; I <files. length; I ++ ){
If (files [I]. isdirectory ()){
Getfilelist (files [I]. getabsolutepath (), filelist );
} Else {
String strfilename = files [I]. getabsolutepath (). tolowercase ();
// System. Out. println ("---" + strfilename );
Filelist. Add (files [I]. getabsolutepath ());
}
}
Return filelist;
}
/**
* Get the list of objects of the specified type in the directory.
* @ Param strpath directory
* @ Param suffix file suffix such as "Zip"
* @ Return file list
*/
Public static arraylist getfilelist (string strpath, string suffix ){
Arraylist filelist = NULL;
Return getfilelist (strpath, suffix, filelist );
}
Private Static arraylist getfilelist (string strpath, string suffix, arraylist filelist ){
If (filelist = NULL ){
Filelist = new arraylist ();
}
File dir = new file (strpath );
File [] files = dir. listfiles ();
If (Files = NULL) return NULL;
For (INT I = 0; I <files. length; I ++ ){
If (files [I]. isdirectory ()){
Getfilelist (files [I]. getabsolutepath (), suffix, filelist );
} Else {
String strfilename = files [I]. getabsolutepath (). tolowercase ();
If (fileutil. getfilesuffix (strfilename). tolowercase (). Equals (suffix )){
Filelist. Add (files [I]. getabsolutepath ());
}
}
}
Return filelist;
}
/**
* Get the file suffix
* @ Param filename complete file name
* @ Return
*/
Public static string getfilesuffix (string filename ){
File F = new file (filename );
String name = f. getname ();
Return name. substring (name. lastindexof (".") + 1 );
}
/**
* Get the file name.
* @ Param filename complete file name
* @ Return
*/
Public static string getfilename (string filename ){
File F = new file (filename );
Return F. getname ();
}
/**
* Get the file name without a suffix.
* @ Param filename complete file name
* @ Return
*/
Public static string getfilenamewithoutsuffix (string filename ){
File F = new file (filename );
String name = f. getname ();
Return name. substring (0, name. lastindexof ("."));
}
/**
* Get the file path after removing the file suffix, which can be used as the path after decompression
* For example, in: e:/test.zip out: e:/test
* @ Param filename complete file name
* @ Return
*/
Public static string getfilepathwithoutsuffix (string filename ){
Return filename. substring (0, filename. lastindexof ("."));
}
}