Java implementation file copy, cut file and delete sample _java

Source: Internet
Author: User
Tags file copy file separator

Copy Code code as follows:

Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;

/**
* Java implementation file copy, cut, delete operations
* file refers to file or folder
* File separator uniform with "\"
*/

public class Fileoperatedemo {

/**
* Copy files or folders
* @param srcpath The path to the source file or source folder
* @param the directory where the Destdir destination files are located
* @return
*/
public static Boolean Copygeneralfile (String Srcpath, String destdir) {
Boolean flag = false;
File File = new file (Srcpath);
if (!file.exists ()) {//source file or source folder does not exist
return false;
}

if (File.isfile ()) {//File copy
Flag = CopyFile (Srcpath, Destdir);
}
else if (file.isdirectory ()) {//folder replication
Flag = CopyDirectory (Srcpath, Destdir);
}

return flag;
}

/**
* Default Copy file method, default overwrite file with same name under target folder
* @param Srcpath
* Source file Absolute path
* @param destdir
* Destination file directory
* @return Boolean
*/
public static Boolean CopyFile (String Srcpath, String destdir) {
Return CopyFile (Srcpath, Destdir, true/**overwriteexistfile*/); Default overwrite file with same name
}

/**
* Default Copy Folder method, default will overwrite folder with same name under target folder
* @param srcpath source folder path
* @param destpath Destination Folder directory
* @return Boolean
*/
public static Boolean CopyDirectory (String Srcpath, String destdir) {
Return CopyDirectory (Srcpath, Destdir, true/**overwriteexistdir*/);
}

/**
* Copy files to target directory
* @param Srcpath
* Source file Absolute path
* @param destdir
* Destination file directory
* @param overwriteexistfile
* Overwrite files with same name under target directory
* @return Boolean
*/
public static Boolean CopyFile (String Srcpath, String Destdir, Boolean overwriteexistfile) {
Boolean flag = false;

File Srcfile = new file (Srcpath);
if (!srcfile.exists () | | |!srcfile.isfile ()) {//source file does not exist
return false;
}

Get file names for files to be copied
String fileName = Srcfile.getname ();
String DestPath = Destdir + File.separator +filename;
File DestFile = new file (destpath);
if (Destfile.getabsolutepath (). Equals (Srcfile.getabsolutepath ())) {//source file path and destination file path duplicate
return false;
}
if (destfile.exists () &&!overwriteexistfile) {//The target directory already has a file with the same name and is not allowed to overwrite
return false;
}

File Destfiledir = new file (Destdir);
if (!destfiledir.exists () &&!destfiledir.mkdirs ()) {//directory does not exist and directory creation failed to return directly
return false;
}

try {
FileInputStream fis = new FileInputStream (Srcpath);
FileOutputStream fos = new FileOutputStream (destfile);
byte[] buf = new byte[1024];
int C;
while ((c = Fis.read (BUF))!=-1) {
Fos.write (buf, 0, C);
}
Fos.flush ();
Fis.close ();
Fos.close ();

Flag = true;
catch (IOException e) {
E.printstacktrace ();
}

return flag;
}

/**
*
* @param srcpath source folder path
* @param destpath Destination Folder directory
* @return
*/
public static Boolean CopyDirectory (String Srcpath, String Destdir, Boolean Overwriteexistdir) {
if (Destdir.contains (Srcpath))
return false;

Boolean flag = false;

File Srcfile = new file (Srcpath);
if (!srcfile.exists () | |!srcfile.isdirectory ()) {//source folder does not exist
return false;
}

Get the name of the folder you want to copy, such as "e:\\dir\\" for the folder you want to copy, and get the name "dir."
String dirname = Srcfile.getname ();

The full path to the destination folder
String Destdirpath = destdir + file.separator + dirname + file.separator;
File Destdirfile = new file (Destdirpath);
if (Destdirfile.getabsolutepath (). Equals (Srcfile.getabsolutepath ())) {
return false;
}
if (destdirfile.exists () && destdirfile.isdirectory () &&!overwriteexistdir) {// The target location has a folder with the same name and does not allow overwriting of folders of the same name, returns false directly
return false;
}

if (!destdirfile.exists () &&!destdirfile.mkdirs ()) {//If the target directory does not exist and the directory creation failed
return false;
}

file[] filelist = Srcfile.listfiles (); Get sub files and subfolders under source folder
if (filelist.length==0) {//If the source folder is empty directory set flag to true directly, this step is very covert and debug for a long time
Flag = true;
}
else {
for (File temp:filelist) {
if (Temp.isfile ()) {//File
Flag = CopyFile (Temp.getabsolutepath (), Destdirpath, Overwriteexistdir); Overriding attributes are also inherited when recursive replication
}
else if (temp.isdirectory ()) {//Folder
Flag = CopyDirectory (Temp.getabsolutepath (), Destdirpath, Overwriteexistdir); Overriding attributes are also inherited when recursive replication
}

if (!flag) {
Break
}
}
}

return flag;
}

/**
* Delete files or folders
* @param path
* Absolute path of the file to be deleted
* @return Boolean
*/
public static Boolean DeleteFile (String path) {
Boolean flag = false;

File File = new file (path);
if (!file.exists ()) {//file does not exist direct return
return flag;
}

Flag = File.delete ();

return flag;
}


/**
* from the above method to extend the Cut method: Copy + DELETE
* @param Destdir ditto
*/
public static Boolean Cutgeneralfile (String Srcpath, String destdir) {
Boolean flag = false;
if (Copygeneralfile (Srcpath, Destdir) && DeleteFile (Srcpath)) {//Copy and delete are successful
Flag = true;
}

return flag;
}

public static void Main (string[] args) {
/** Test Copy File * *
System.out.println (Copygeneralfile ("d://test/test.html", "d://test/test/")); General normal scene
System.out.println (Copygeneralfile ("D://notexistfile", "d://test/")); Copying files or folders that do not exist
System.out.println (Copygeneralfile ("d://test/test.html", "d://test/")); The file to be copied is in the same directory as the destination file
System.out.println (Copygeneralfile ("d://test/test.html", "d://test/test/")); Overwrite file with same name under target directory
System.out.println (CopyFile ("d://test/", "D://test2", false)); Do not overwrite files of the same name under the target directory
System.out.println (Copygeneralfile ("d://test/test.html", "notexist://noexistdir/")); Copy files to a directory that cannot exist or can be created

System.out.println ("---------");

/** Test Replication Folder * *
System.out.println (Copygeneralfile ("d://test/", "d://test2/"));

System.out.println ("---------");

/** Test Delete File * *
System.out.println (DeleteFile ("d://a/"));
}

}

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.