Additions and deletions to Java files and directories

Source: Internet
Author: User

When you use Java for development, you often use the methods of adding and deleting files and directories. I wrote a Gadget class. To share with you, I hope you correct me:

Package Com.wangpeng.utill;import Java.io.file;import Java.io.fileinputstream;import java.io.FileOutputStream; Import Java.io.filewriter;import java.io.inputstream;import java.io.printwriter;/** * @author Wangpeng * */public class Tooloffile {/** * Create directory * * @param folderpath * Directory directory * @throws Exception */public static void NewFolder (String FolderPath) throws Exception {try {java.io.File myFolder = new Java.io.File (folderpath); if (!myfolder.exists ()) { Myfolder.mkdir ();}} catch (Exception e) {throw e;}}  /** * Create File * * @param filePath * File path * @throws Exception */public static void NewFile (String filePath) throws Exception {try {File myFile = new File (FilePath), if (!myfile.exists ()) {myfile.createnewfile ();}} catch (Exception e) {th Row e;}} /** * Create file and write content * * @param filePath * File path * @param filecontent * File content written * @throws Exception */p ublic static void NewFile (String filePath, String filecontent) throws Exception {//handy class for writing character Files FileWriter filEwriter = null;//A formatted representation of the printed object to the text output stream, creating a new PrintWriter with the specified file that does not have its own active row refresh PrintWriter = null;try {File MyFile = new file (filep ATH); if (!myfile.exists ()) {myfile.createnewfile ();} FileWriter = new FileWriter (myFile);p rintwriter = new PrintWriter (fileWriter);p rintwriter.print (filecontent); Printwriter.flush ();} catch (Exception e) {throw e;} finally {if (printwriter! = null) {Printwriter.close ();} if (fileWriter! = null) {Filewriter.close ();}}} /** * Copy Files * * @param oldpath * Copied files * @param newpath * Copied to the file * @throws Exception */public Stati c void CopyFile (String oldpath, String NewPath) throws Exception {InputStream instream = null; FileOutputStream OutStream = null;try {int byteread = 0; File Oldfile = new file (oldpath);//if (Oldfile.exists ()) {instream = new FileInputStream (oldfile) Leoutputstream (NewPath); byte[] buffer = new Byte[1444];while ((Byteread = instream.read (buffer))! =-1) {Outstream.write (buffer, 0, byteread);} Outstream.flush ();}} Catch (Exception e) {throw e;} finally {if (OutStream! = null) {Outstream.close ();} if (instream! = null) {Instream.close ();}}} /** * Copy file * @param instream the input stream of the copied file * @param newpath is copied to the target * @throws Exception */public static void CopyFile (INPUTST Ream instream, String NewPath) throws Exception {FileOutputStream OutStream = null;try {int byteread = 0;outstream = new Fi Leoutputstream (NewPath); byte[] buffer = new Byte[1444];while ((Byteread = instream.read (buffer))! =-1) {Outstream.write (buffer, 0, byteread);} Outstream.flush ();} catch (Exception e) {throw e;} finally {if (OutStream! = null) {Outstream.close ();} if (instream! = null) {Instream.close ();}}} /** * Copy directory * * @param oldpath * Copied directory path * @param newpath * Directory path copied to * @throws Exception */public The static void CopyFolder (String oldpath, String NewPath) throws Exception {try {(new File (NewPath)). Mkdirs ();//Assuming the directory does not exist create a new Directory file A = new file (OldPath); string[] File = A.list (); File Tempin = null;for (int i = 0; i < FILE.LEngth; i++) {if (Oldpath.endswith (File.separator)) {tempin = new file (OldPath + file[i]);} else {tempin = new file (OldPath + file . Separator + file[i]);} if (Tempin.isfile ()) {CopyFile (Tempin.getabsolutepath (), NewPath + "/" + (Tempin.getname ()). ToString ());} else if ( Tempin.isdirectory ()) {//assumed to be subdirectory CopyFolder (OldPath + "/" + file[i], NewPath + "/" + File[i]);}} catch (Exception e) {throw e;}} /** * Delete File * * @param filepathandname */public static void Delfilex (String filepathandname) {File Mydelfile = new File (fi Lepathandname); Mydelfile.delete ();} /** * Delete directory * * @param path */public static void Delforder (String path) {File Deldir = new File (path); if (!deldir.exists () ) {return;} if (!deldir.isdirectory ()) {return;} string[] templist = Deldir.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 ();} else if (temp. Isdirectory ()) {//delete all contents Delforder (path + "/" + Templist[i]);}} Deldir.delete ();} public static void Main (string[] args) {String OldPath = "f:/test/aaa/"; String NewPath = "f:/test/bbb/"; try {//Tooloffile.newfolder ("f:/test/hello/");//Tooloffile.newfile ("f:/test/hello/ World.txt "," I love you, the world!

"); Tooloffile.copyfolder (OldPath, NewPath);//Tooloffile.delforder ("F:/test/hello");} catch (Exception e) {e.printstacktrace ();} System.out.println ("OK");}}


Additions and deletions to Java files and directories

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.