Java File Operation example

Source: Internet
Author: User
Package COM. file. sample; import Java. io. ioexception; import Java. io. *; public class fileoperate {public fileoperate () {}/ *** create a directory ** @ Param folderpath * string such as C: /fqf * @ return Boolean */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 () ;}/ *** Create File *** @ Param filepathandname *** string file path and name, for example, C: /fqf.txt * @ Param filecontent * string file content ** @ return Boolean */Public void newfile (string filepathandname, string filecontent) {try {string filepath = filepathandname; filepath = filepath. tostring (); file Myf Ilepath = 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 () ;}/ ** delete file * @ Param filepathandname * string file path and name such as C:/fqf.txt * @ Param filecontent * String * @ return Boolean */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 () ;}/ *** Delete folder ** @ Param filepathandname * string folder path and name such as C:/fqf * @ Param filecontent * string * @ Return Boolean */Public void delfolder (string folderpath) {try {delallfile (folderpath); // After deleting all content, 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 ("An error occurred while deleting Folders"); E. printstacktrace () ;}}/*** delete all files in the folder * @ Param path * string folder path such as C:/fqf */Public Void delallfile (string path) {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 file delfolder (path + "/ "+ Templist [I]); // Delete the empty folder}/*** copy a single file ** @ Param oldpath * string original file path such as: C: /fqf.txt * @ Param newpath * string: path after copying, for example, F:/fqf.txt * @ return Boolean */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 fileoutputstrea M fs = new fileoutputstream (newpath); byte [] buffer = new byte [1444]; int length; while (byteread = instream. Read (buffer ))! =-1) {bytesum + = byteread; // number of bytes file size 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 () ;}/ *** copy the entire folder content ** @ Param oldpath * string original file path, for example, C: /fqf * @ Param newpath * string copied path, for example, F:/fqf/FF * @ return Boolean */Public void copyfolder (string oldpath, string newpath) {try {(New fil E (newpath )). mkdirs (); // create a new folder file a = new file (oldpath) if the folder does not exist; string [] file =. 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 file Outputstream (newpath + "/" + (temp. getname ()). tostring (); byte [] B = new byte [1024*5]; int Len; while (LEN = input. read (B ))! =-1) {output. write (B, 0, Len);} output. flush (); output. close (); input. close ();} If (temp. isdirectory () {// For subfolders copyfolder (oldpath + "/" + file [I], newpath + "/" + file [I]);} catch (exception e) {system. out. println ("An error occurred while copying the entire folder"); E. printstacktrace () ;}/ *** move the file to the specified directory ** @ Param oldpath * string such as: C:/fqf.txt * @ Param newpath * string such as: D: /fqf.txt */Public void movefile (string oldpath, string newpath) {copyfile (oldpath, newpath); delfile (oldpath );} /*** move the file to the specified directory ** @ Param oldpath * string such as: C:/fqf.txt * @ Param newpath * string such as: D: /fqf.txt */Public void movefolder (string oldpath, string newpath) {copyfolder (oldpath, newpath); delfolder (oldpath);} open the folder (operate the Windows folder) try {string [] cmd = new string [5]; cmd [0] = "cmd"; cmd [1] = "/C "; CMD [2] = "start"; cmd [3] = ""; cmd [4] = "D: \ apache-tomcat-7.0.20-windows-x86 \ apache-Tomcat-7.0.20 \ webapps \ Root "invalid runtime.getruntime(cmd.exe C (CMD);} catch (ioexception e) {e. printstacktrace ();}
Related Article

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.