Before Javaeye saw about the Java operation file of an article, write very good, but find a half-day also did not find, will find the bottom several articles to tidy up, do a summary, is a learning backup ...
1, verify that the incoming path is the correct path name (Windows system, other systems are not used)
Java code
- Regular expression that verifies whether a string is the correct pathname
- Private static String matches = "[a-za-z]:\\\\[^:?\" ><*]* ";
- Correct by the return value of the spath.matches (matches) method
- Spath as a path string
2, the common folder or file deletion method, call this method directly, you can delete the folder or file, including all the files under the folder
Java code
- /**
- * Deletes the specified directory or file according to the path, regardless of whether it exists or not
- * @param spath the directory or file to be deleted
- * @return Delete succeeds returns True, otherwise false is returned.
- */
- Public Boolean DeleteFolder (String spath) {
- Flag = false;
- FILE = new file (spath);
- //Determine if a directory or file exists
- if (!file.exists ()) { //does not exist return false
- return flag;
- } Else {
- //Determine if the file is
- if (File.isfile ()) { //is a file when the delete file method is called
- return DeleteFile (spath);
- The Delete directory method is called when the else {//is a directory
- return DeleteDirectory (spath);
- }
- }
- }
3, to implement the method of deleting files,
Java code
- /**
- * Delete individual files
- * @param the file name of spath deleted files
- * @return Single File Delete successfully returns TRUE, otherwise false
- */
- Public Boolean DeleteFile (String spath) {
- Flag = false;
- FILE = new file (spath);
- //path is file and is not empty then delete
- if (file.isfile () && file.exists ()) {
- File.delete ();
- Flag = true;
- }
- return flag;
- }
4, implement the method of deleting the folder,
Java code
- /**
- * Delete directories (folders) and files in directory
- * @param spath The file path of the deleted directory
- * @return Directory Delete successfully returns TRUE, otherwise false
- */
- Public boolean deletedirectory (String spath) {
- //If spath does not end with a file delimiter, the file delimiter is automatically added
- if (!spath.endswith (File.separator)) {
- spath = spath + file.separator;
- }
- File Dirfile = new File (spath);
- //If the dir corresponding file does not exist or is not a directory, exit
- if (!dirfile.exists () | |!dirfile.isdirectory ()) {
- return false;
- }
- Flag = true;
- //Delete all files under the folder (including subdirectories)
- file[] files = dirfile.listfiles ();
- For (int i = 0; i < files.length; i++) {
- //Delete sub-Files
- if (Files[i].isfile ()) {
- Flag = DeleteFile (Files[i].getabsolutepath ());
- if (!flag) break ;
- } //delete sub-directories
- else {
- Flag = DeleteDirectory (Files[i].getabsolutepath ());
- if (!flag) break ;
- }
- }
- if (!flag) return false;
- //Delete current directory
- if (Dirfile.delete ()) {
- return true;
- } Else {
- return false;
- }
- }
5,main () method
Java code
- Public static void Main (string[] args) {
- Handlefileclass HFC = new Handlefileclass ();
- String path = "D:\\abc\\123\\ab1";
- Boolean result = HFC. CreateFolder (path);
- SYSTEM.OUT.PRINTLN (result);
- Path = "d:\\abc\\124";
- result = HFC. DeleteFolder (path);
- SYSTEM.OUT.PRINTLN (result);
- }
The main () method only makes a simple test, the establishment of folders and files are locally established, the situation should be considered very comprehensive, including folders containing folders, files. Different cases of the file .....
There are no problems with the implementation, and folders and files can be deleted correctly.
For other types of files, continue to learn .....
Java Delete folders and files