Java How to create a directory (delete/modify/copy directory and file) code instance _java

Source: Internet
Author: User

Copy Code code as follows:

Import java.io.*;

public class Fileoperate {
Public Fileoperate () {
}

 /**
   * New directory
   * @param folderpath String such as C:/FQF
   * @return bool EAN
   *
  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 ("Error in new directory operation");
      e.printstacktrace ();
   }
 }

 /**
   * New file
   * @param filepathandname String file path and name such as C:/fqf.txt
   ; * @param filecontent String file content
   * @return Boolean
   * *
  public void NewFile (STR ing 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 ("New directory Operation 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 ("Delete file operation 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); Delete all contents inside
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 Error");
E.printstacktrace ();

}

}

/**
* Delete all files inside the folder
* @param path String folder paths such as C:/fqf
*/
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 files in folder first
Delfolder (path+ "/" + templist[i])//Delete empty folder
}
}
}

/**
* Copy individual files
* @param oldpath String original file path such as: C:/fqf.txt
* @param newpath String Copy the path after: 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 ()) {//File exists
InputStream instream = new FileInputStream (OldPath); Read into the original file
FileOutputStream fs = new FileOutputStream (NewPath);
byte[] buffer = new byte[1444];
int length;
while ((Byteread = instream.read (buffer))!=-1) {
Bytesum + = Byteread; Byte Number file size
System.out.println (bytesum);
Fs.write (buffer, 0, byteread);
}
Instream.close ();
}
}
catch (Exception e) {
System.out.println ("Copy single File operation Error");
E.printstacktrace ();

}

}

/**
* Copy entire folder contents
* @param oldpath String original file path such as: C:/FQF
* @param newpath String Copy the path after: f:/fqf/ff
* @return Boolean
*/
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[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 ("Copy entire folder contents operation Error");
E.printstacktrace ();

}

}

/**
* Move files to 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 files to 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);

}
}

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.