Java Cut Copy File

Source: Internet
Author: User

Package servlet.back;
Import java.io.*;

Import org.dom4j.Document;
Import org.dom4j.DocumentException;
Import org.dom4j.Element;
Import Org.dom4j.io.SAXReader;

Import service. Tools;

public class Jqwj {
Public Jqwj () {

}

public static void Main (String args[]) {
Autocut ();
NewFolder ("d:/100");
MoveFile ("d:/100/new text document. txt", "D:/FQ");
}
/*
* Automatically move files
*/
public static void Autocut () {
Document doc = null;
Element root = null;
String Folderpath=tools.getpath ();
try {
FolderPath = Java.net.URLDecoder.decode (FolderPath, "GBK");
doc = new Saxreader (). Read (New File (FolderPath, "connectionconfig.xml"));
root = Doc.getrootelement (). Element ("QZSJGL");
String cutfile = root.element ("cutfile") = = null? "False": Root.element ("Cutfile"). Gettexttrim ();
String time = root.element ("time") = = null? "False": Root.element ("Time"). Gettexttrim ();
System.out.println ("Cutfile:" +cutfile+ "\ttime:" +time);


File Fouthpath = new file (This.getservletcontext (). Getrealpath ("") +file.separator+ "Uploadfiles");

} catch (Unsupportedencodingexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (Documentexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
/**
* New Directory
* @param folderpath String such as C:/fqf
* @return Boolean
*/
public static 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 such as C:/fqf.txt
* @param filecontent String file contents
* @return Boolean
*/
public static void NewFile (String 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 Files
* @param filepathandname String file path and name such as C:/fqf.txt
* @param filecontent String
* @return Boolean
*/
public static 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 ("Error deleting file operation");
E.printstacktrace ();

}

}

/**
* Delete Folder
* @param filepathandname String folder path and name such as C:/FQF
* @param filecontent String
* @return Boolean
*/
public static void Delfolder (String folderpath) {
try {
Delallfile (FolderPath); Delete all content inside
String FilePath = FolderPath;
FilePath = Filepath.tostring ();
Java.io.File Myfilepath = new Java.io.File (FilePath);
Myfilepath.delete (); Delete Empty folders

}
catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Error deleting folder operation");
E.printstacktrace ();

}

}

/**
* Delete all files in the folder
* @param path String folder path such as C:/FQF
*/
public static void Delallfile (String Pat h) {
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 the file inside the folder
Delfolder (path+ "/" + Templ Ist[i]);//delete empty folder
}
}
}

/**
* Copy individual files
* @param oldpath String original file path such as: C:/fqf.txt
* @param NewPath String After copying the path such as: F:/fqf.txt
* @return Boolean
*/
public static 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 in the original file
FileOutputStream 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 ("Error copying Single file operation");
E.printstacktrace ();

}

}

/**
* Copy entire folder contents
* @param oldpath String original file path such as: C:/FQF
* @param NewPath String After copying the path such as: F:/FQF/FF
* @return Boolean
*/
public static 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);
if (A.isdirectory ()) {
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] ");
}
}
}else{
//
// }

}
catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Error copying entire folder contents operation");
E.printstacktrace ();

}

}

/**
* Move files to the specified directory
* @param oldpath String such as: C:/fqf.txt
* @param newpath String such as: D:/fqf.txt
*/
public static void MoveFile (String oldpath, String NewPath) {
CopyFile (OldPath, NewPath);
Delfile (OldPath);

}

/**
* Move files to the specified directory
* @param oldpath String such as: C:/fqf.txt
* @param newpath String such as: D:/fqf.txt
*/
public static void MoveFolder (String oldpath, String NewPath) {
CopyFolder (OldPath, NewPath);
Delfolder (OldPath);

}
}

Java Cut Copy File

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.