創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買多久送多久,劃算不套路!
創(chuàng)新互聯(lián):于2013年成立為各行業(yè)開拓出企業(yè)自己的“網(wǎng)站建設(shè)”服務(wù),為近千家公司企業(yè)提供了專業(yè)的成都做網(wǎng)站、成都網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)和網(wǎng)站推廣服務(wù), 按需制作網(wǎng)站由設(shè)計(jì)師親自精心設(shè)計(jì),設(shè)計(jì)的效果完全按照客戶的要求,并適當(dāng)?shù)奶岢龊侠淼慕ㄗh,擁有的視覺效果,策劃師分析客戶的同行競(jìng)爭(zhēng)對(duì)手,根據(jù)客戶的實(shí)際情況給出合理的網(wǎng)站構(gòu)架,制作客戶同行業(yè)具有領(lǐng)先地位的。整理文檔,搜刮出一個(gè)Java實(shí)現(xiàn)文件或文件夾的復(fù)制到指定目錄的代碼,稍微整理精簡一下做下分享。
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class Test { private static int a = 5; public static void main(String[] args) { //需要復(fù)制的目標(biāo)文件或目標(biāo)文件夾 String pathname = "C:/Users/likun/Desktop/git_project"; File file = new File(pathname); //復(fù)制到的位置 String topathname = "C:/Users/likun/Desktop/movie"; File toFile = new File(topathname); try { copy(file, toFile); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void copy(File file, File toFile) throws Exception { byte[] b = new byte[1024]; int a; FileInputStream fis; FileOutputStream fos; if (file.isDirectory()) { String filepath = file.getAbsolutePath(); filepath=filepath.replaceAll("\\\\", "/"); String toFilepath = toFile.getAbsolutePath(); toFilepath=toFilepath.replaceAll("\\\\", "/"); int lastIndexOf = filepath.lastIndexOf("/"); toFilepath = toFilepath + filepath.substring(lastIndexOf ,filepath.length()); File copy=new File(toFilepath); //復(fù)制文件夾 if (!copy.exists()) { copy.mkdir(); } //遍歷文件夾 for (File f : file.listFiles()) { copy(f, copy); } } else { if (toFile.isDirectory()) { String filepath = file.getAbsolutePath(); filepath=filepath.replaceAll("\\\\", "/"); String toFilepath = toFile.getAbsolutePath(); toFilepath=toFilepath.replaceAll("\\\\", "/"); int lastIndexOf = filepath.lastIndexOf("/"); toFilepath = toFilepath + filepath.substring(lastIndexOf ,filepath.length()); //寫文件 File newFile = new File(toFilepath); fis = new FileInputStream(file); fos = new FileOutputStream(newFile); while ((a = fis.read(b)) != -1) { fos.write(b, 0, a); } } else { //寫文件 fis = new FileInputStream(file); fos = new FileOutputStream(toFile); while ((a = fis.read(b)) != -1) { fos.write(b, 0, a); } } } } }