Recently I have summarized the methods for processing files in Java, including file creation, deletion, renaming, copying, and moving,
The Code is as follows:
Package COM. file; import Java. io. bufferedreader; import Java. io. bufferedwriter; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. filereader; import Java. io. filewriter; import Java. io. ioexception; import Java. util. date; public class file_test {// whether the file is successfully deleted // Private Boolean flag;/*** create a file and directory * @ Param path * @ Param filename */Public void create_file (string path, string filename) {string sparator = file. separator; file F = new file (path, filename); If (F. exists () {system. out. println ("file exist! File detail "); system. out. println ("file path" + F. getabsolutepath (); system. out. println ("file length" + F. length () + "" + F. lastmodified ();} else {f. getparentfile (). mkdirs (); try {f. createnewfile ();} catch (ioexception e) {e. printstacktrace (); system. out. println ("file creation failed! ") ;}}/*** Get file attributes * @ Param path * @ Param filename */Public void GetFile (string path, string filename) {file F = new file (path, filename); If (F. exists () {system. out. println ("file abspath" + F. getabsolutepath () + '\ n' + "file reable" + F. getparentfile () + '\ n' + "file standar" + F. isfile () + '\ n' + "file length" + F. length () + '\ n' + "file modify time" + new date (F. lastmodified () + '\ n' + "file can read" + F. canread () + '\ n' + "File Can write "+ F. canwrite () + '\ n' + "file whether hidden" + F. ishidden ();} else {system. out. println ("file not exist! ") ;}}/*** Copy files and folders * @ Param SRC * @ Param des * @ return */Public Boolean copy (string SRC, string des) {file in = new file (SRC); file out = new file (DES); If (! In. exists () {system. Out. println (in. getabsolutepath () + "source file path Error !!! "); Return false;} else {system. out. println ("source file path" + in. getabsolutepath (); system. out. println ("target path" + out. getabsolutepath ();} If (! Out. exists () out. mkdirs (); file [] file = in. listfiles (); fileinputstream fin = NULL; fileoutputstream fout = NULL; For (INT I = 0; I <file. length; I ++) {If (file [I]. isfile () {try {fin = new fileinputstream (file [I]);} catch (filenotfoundexception e) {e. printstacktrace ();} system. out. println ("in. name = "+ file [I]. getname (); try {fout = new fileoutputstream (new file (des + file. separator + file [I]. getname ();} catch (filenotfound Exception e) {e. printstacktrace ();} system. out. println (DES); int C; byte [] B = new byte [1024]; try {While (C = fin. read (B ))! =-1) {fout. Write (B, 0, c); system. Out. println ("copy file! ");} Fin. Close (); fout. Flush (); fout. Close (); system. Out. println (" copied! ");} Catch (ioexception e) {e. printstacktrace () ;}} else copy (SRC + file. separator + file [I]. getname (), des + file. separator + file [I]. getname ();} return false;}/*** folder movement * @ Param SRC * @ Param DEST * @ return */private Boolean movefolder (string SRC, string DEST) {file srcfolder = new file (SRC); file destfolder = new file (DEST); If (! Srcfolder. exists () {return false;} else {If (! Destfolder. exists () destfolder. mkdirs (); If (srcfolder. isfile () {srcfolder. renameto (new file (DEST + srcfolder. getname ();} else if (srcfolder. isdirectory () {file [] files = srcfolder. listfiles (); For (INT I = 0; I <files. length; I ++) {If (files [I]. isfile () files [I]. renameto (new file (DEST + file. separator + files [I]. getname (); else movefolder (SRC + file. separator + files [I]. getname (), DEST + file. separator + files [I ]. Getname () ;}}return true;}/*** move a single file */private void filemove () {string src = "E:/TT /", des = "F:/KK/"; file F = new file (DES); file filelist [] = f. listfiles (); For (INT I = 0; I <filelist. length; I ++) {filelist [I]. renameto (new file (SRC + filelist [I]. getname () ;}}/*** delete a specified directory or file based on the path, whether it exists or not * @ Param Spath indicates the directory or file to be deleted * @ return returns true if the deletion is successful. Otherwise, false is returned. */Private Boolean deletefolder (string Spath) {Boolean flag = false; file = new file (Spath); // determine whether a directory or file exists if (! File. exists () {// returns false return flag;} else {// determines whether it is a file if (file. isfile () {// return deletefile (Spath );} when else {// is a directory, call the delete directory method return deletedirectory (Spath );}}} /*** delete a single file * @ Param Spath File Name of the deleted file * @ return true if the deletion of a single file is successful; otherwise, false */private Boolean deletefile (string Spath) is returned) {Boolean flag = false; file = new file (Spath); // Delete if (file. isfile () & fi Le. exists () {file. delete (); flag = true;} return flag;}/*** Delete directory (folder) and the file in the directory * @ Param Spath is the file path of the deleted directory * @ return returns true if the directory is deleted successfully; otherwise, false */private Boolean deletedirectory (string Spath) is returned) {// If Spath does not end with a file separator, the file separator if (! Spath. endswith (file. separator) {Spath = Spath + file. separator;} file dirfile = new file (Spath); // exit if (! Dirfile. exists () |! Dirfile. isdirectory () {return false;} Boolean flag = true; // delete all files in the folder (including subdirectories) file [] files = dirfile. listfiles (); For (INT I = 0; I <files. length; I ++) {// Delete the sub-file if (files [I]. isfile () {flag = deletefile (files [I]. getabsolutepath (); If (! Flag) break;} // Delete the subdirectory else {flag = deletedirectory (files [I]. getabsolutepath (); If (! Flag) break;} If (! Flag) return false; // Delete the current directory if (dirfile. delete () {system. out. println ("deleted successfully"); Return true;} else {system. out. println ("failed to delete"); Return false ;}/ *** displays the file name in binary tree format (in Windows File Management format) * @ Param file * @ Param level */private void treename (File file, int level) {string Space = ""; for (int t = 0; t <level; t ++) {space + = "";} file [] files = file. listfiles (); For (INT I = 0; I <files. length; I ++) {system. out. println (space + files [I]. getname (); If (files [I]. isdirectory () {treename (files [I], level + 1) ;}} public static void main (string [] ARGs) {file_test Ft = new file_test (); /* ft. create_file (path, "B .txt"); ft. list_file ("F:/a"); ft. getFile (path, "a.txt"); ft. copy_file (path, "a.txt", path2, "c.txt"); * // ft. copy ("F:/11", "F:/KK"); // ft. deletefolder ("F:/KK"); ft. deletefolder ("E:/TT"); // ft. movefolder ("F:/TT/", "F:/KK /");}}
Write specific text information to the root directory of the project:
String str="write dir";String filename=System.getProperty("user.dir")+"/src/com/agency/model2/temp.txt";File f=new File(filename);FileWriter fw=new FileWriter(f);fw.write(str);fw.flush();fw.close();
The above information can be recorded in the project file.
OK. The above method has been tested. The function is basic. If you have any questions, please leave a message.