File, folder operation

xiaoxiao2021-03-06  50

** *

Title: Various operations of the file *

Copyright: Copyright (c) 2004 *

Company: Guangdong * @Author Network Information Department celebration Feng * @version 1.0 * /

Package Common;

Import java.io. *;

Public class fileoperate {public fileoperate () {}

/ ** * * @param folderPath String new directory 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 ("New Directory Operation error"); E.PrintStackTrace ();}}

/ ** * New file * @Param filepathandname string file path and name, name, 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 myFilePath = new File (filePath); if {myFilePath.createNewFile () (myFilePath.exists ()!);} FileWriter resultFile = new FileWriter (myFilePath); Printwriter myfile = new printwriter; string strcontent = filecontent; myfile.println (strconte); resultfile.close ();

} Catch (Exception E) {system.out.println ("New Directory Operation error"); E.PrintStackTrace ();

}

}

/ ** * * @param filePathAndName String delete the file path and the file 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 ("Delete File Operation error"); E.PrintStackTrace ();

}

}

/ ** * Delete folder * @Param filepathandname String folder path and name, C: / FQF * @Param FileContent string * @return boolean * / public void Delfolder (String folderpath) {Try {deLAllFile (FolderPath); // Delete all content String filepath = folderpath; filepath = filepath.tostring (); java.io.file myfilepath = new java.io.file (filepath); myfilepath.delete (); // Delete empty folder

} Catch (exception e) {system.out.println ("Delete Folder Operation"); E.PrintStackTrace ();

}

}

/ ** * Remove all files in the folder * @Param path string folder path such as C: / fqf * / public void deLallFile (String path) {file file = new file (path); if (! File.exists )) {Return;} f (! File.Indirectory ()) {return;} string [] templist = file.list (); file temp = null; for (int i = 0; i

}

}

/ ** * Copy the entire folder content * @Param OldPath String Original file path, such as: c: / fqf * @Param NewPath String replication path, like: f: / fqf / ff * @return bolean * / public void copyfolder (String OldPath, String newPath) {Try {(new file ()). MKDIRS (); // If the folder does not exist, create a new folder file a = new file (OldPath); String [] file = a.list FILE TEMP = NULL; for (int i = 0; i

if (temp.isFile ()) {FileInputStream input = new FileInputStream (temp); FileOutputStream output = new FileOutputStream (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.Indirectory ()) {// If the subfolder CopyFolder (OldPath "/" file [i], newpath "/" file [i]);}}} Catch (Exception E) {system.out.println ("Copy the entire folder content error"; E.PrintStackTrace ();

}

}

/ ** * Mobile files to the specified directory * @Param oldpath string such as: c: /fqf.txt * @Param NewPath String such as: D: /Fqf.txt * / public void file (string oldpath, string newpath) {copyfile OldPath, NewPath; Delfile (OldPath);

}

/ ** * Mobile folder to the specified directory * @Param oldpath string such as: c: / fqf * @Param newPath string such as: D: / fqf * / public voidfolder (String Oldpath, String newPath) {CopyFolder (OldPath, NewPath) ); Delfolder (OldPath);}}

转载请注明原文地址:https://www.9cbs.com/read-116101.html

New Post(0)