Java File Operations

Source: Internet
Author: User
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 ("."));
}
}

 

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.