Dark Horse programmer-java. Io. File Operations

Source: Internet
Author: User

 

----------------------
Android training and Java training. We look forward to communicating with you! ----------------------   Through this program, you can understand the operations of various files in the Java. Io. File class: Import java. Io. *; public class fileoperate { Public fileoperate (){  }  Public void newfolder (string folderpath ){  Try {  String filepath = folderpath;  Filepath = filepath. tostring ();  Java. Io. File myfilepath = new java. Io. File (filepath );  If (! Myfilepath. exists ()){  Myfilepath. mkdir ();  }  }  Catch (exception e ){  System. Out. println ("Directory creation error ");  E. printstacktrace ();  }  }    Public void newfile (string filepathandname, string filecontent ){  Try {  String filepath = filepathandname;  Filepath = filepath. tostring ();  File myfilepath = new file (filepath );  If (! Myfilepath. exists ()){  Myfilepath. createnewfile ();  }  Filewriter resultfile = new filewriter (myfilepath );  Printwriter myfile = new printwriter (resultfile );  String strcontent = filecontent;  Myfile. println (strcontent );  Resultfile. Close ();  }  Catch (exception e ){  System. Out. println ("Directory creation error ");  E. printstacktrace ();  }  }    Public void delfile (string filepathandname ){  Try {  String filepath = filepathandname;  Filepath = filepath. tostring ();  Java. Io. File mydelfile = new java. Io. File (filepath );  Mydelfile. Delete ();  }  Catch (exception e ){  System. Out. println ("File Deletion error ");  E. printstacktrace ();  }  }    Public void delfolder (string folderpath ){  Try {  Delallfile (folderpath); // delete all contents  String filepath = folderpath;  Filepath = filepath. tostring ();  Java. Io. File myfilepath = new java. Io. File (filepath );  Myfilepath. Delete (); // delete an empty folder  }  Catch (exception e ){  System. Out. println ("Folder deletion error ");  E. printstacktrace ();  }  }    Public void delallfile (string path ){  File file = new file (PATH );  If (! File. exists ()){  Return;  }  If (! File. isdirectory ()){  Return;  }  String [] templist = file. List ();  File temp = NULL;  For (INT I = 0; I <templist. length; I ++ ){  If (path. endswith (file. separator )){  Temp = new file (path + templist [I]);  }  Else {  Temp = new file (path + file. Separator + templist [I]);  }  If (temp. isfile ()){  Temp. Delete ();  }  If (temp. isdirectory ()){  Delallfile (path + "/" + templist [I]); // delete the files in the folder first  Delfolder (path + "/" + templist [I]); // Delete the empty folder.  }  }  }    Public void copyfile (string oldpath, string newpath ){  Try {  Int bytesum = 0;  Int byteread = 0;  File oldfile = new file (oldpath );  If (oldfile. exists () {// when the file exists  Inputstream instream = new fileinputstream (oldpath); // read the original file  Fileoutputstream FS = new fileoutputstream (newpath );  Byte [] buffer = new byte [1444];  Int length;  While (byteread = instream. Read (buffer ))! =-1 ){  Bytesum + = byteread; // file size in bytes  System. Out. println (bytesum );  FS. Write (buffer, 0, byteread );  }  Instream. Close ();  }  }  Catch (exception e ){  System. Out. println ("An error occurred while copying a single file ");  E. printstacktrace ();  }  }    Public void copyfolder (string oldpath, string newpath ){  Try {  (New file (newpath). mkdirs (); // create a new folder if the folder does not exist  File a = new file (oldpath );  String [] file = A. List ();  File temp = NULL;  For (INT I = 0; I <file. length; I ++ ){  If (oldpath. endswith (file. separator )){  Temp = new file (oldpath + file [I]);  }  Else {  Temp = new file (oldpath + file. Separator + file [I]);  }  If (temp. isfile ()){  Fileinputstream input = new fileinputstream (temp );  Fileoutputstream output = new fileoutputstream (newpath + "/" +  (Temp. getname (). tostring ());  Byte [] B = new byte [1, 1024*5];  Int Len;  While (LEN = input. Read (B ))! =-1 ){  Output. Write (B, 0, Len );  }  Output. Flush ();  Output. Close ();  Input. Close ();  }  If (temp. isdirectory () {// if it is a subfolder  Copyfolder (oldpath + "/" + file [I], newpath + "/" + file [I]);  }  }  }  Catch (exception e ){  System. Out. println ("An error occurred while copying the entire folder ");  E. printstacktrace ();  }  }    Public void movefile (string oldpath, string newpath ){  Copyfile (oldpath, newpath );  Delfile (oldpath );  }    Public void movefolder (string oldpath, string newpath ){  Copyfolder (oldpath, newpath );  Delfolder (oldpath );  }} ---------------------- Android training, Java training, and hope to communicate with you! ----------------------

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.