Java copy files to another directory

Source: Internet
Author: User

Package com.util;

Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;

public class Testhtml {
/**
* 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 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 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 ("Error copying entire folder contents operation");
E.printstacktrace ();

}

}
public static void Main (string[] args) throws Exception {
////This is your source file, which itself is present
//file beforefile = new File (" C:/users/administrator/desktop/untitled-2.html ");
//
////This is the file you want to save, it's custom, it doesn't exist
//file afterfile = new File ("c:/users/administrator/desktop/jiekou0/ Untitled-2.html "); The
//
////defines the file input stream that is used to read the Beforefile file
//fileinputstream fis = new FileInputStream (beforefile);
The
////defines the file output stream, which is used to write information to the Afterfile file
//fileoutputstream fos = new FileOutputStream (afterfile);

////file buffer
//byte[] b = new byte[1024];
Read the file stream information to the file buffer, if the reading result is not 1 means that the file is not read, and the reverse has been read
//while (Fis.read (b)!=-1) {
////write the contents of the buffer in the Afterfile file
//fos.write (b);
//fos.flush ();
//}
String oldpath= "c:/users/administrator/desktop/untitled-2.html";
String newpath= "c:/users/administrator/desktop/jiekou0/untitled-2.html";
testhtml t=new testhtml ();
T.copyfile (OldPath, NewPath);


}
}

Referenced from: http://zhidao.baidu.com/link?url=83SQQKvuLR1aiA_QPPLE7K_ Tfzkiapkuqjfalts6ahvcvtxokzmnayuwrmqkytobqkrjuczjwvpxuqbp1fttyk

Http://www.educity.cn/wenda/214981.html these two

Java copy files to another directory

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.