Java代碼復(fù)制文件夾時(shí),則需要利用Flie類在目標(biāo)文件夾中創(chuàng)建相應(yīng)的目錄,并且使用遞歸方法,代碼如下:
成都創(chuàng)新互聯(lián)公司專注于網(wǎng)站建設(shè)|成都企業(yè)網(wǎng)站維護(hù)|優(yōu)化|托管以及網(wǎng)絡(luò)推廣,積累了大量的網(wǎng)站設(shè)計(jì)與制作經(jīng)驗(yàn),為許多企業(yè)提供了網(wǎng)站定制設(shè)計(jì)服務(wù),案例作品覆蓋展覽展示等行業(yè)。能根據(jù)企業(yè)所處的行業(yè)與銷售的產(chǎn)品,結(jié)合品牌形象的塑造,量身設(shè)計(jì)品質(zhì)網(wǎng)站。
import?java.io.*;??
/**?
*?復(fù)制文件夾或文件夾?
*/??
public?class?CopyDirectory?{??
//?源文件夾???
static?String?url1?=?"F:/photos";??
//?目標(biāo)文件夾???
static?String?url2?=?"D:/tempPhotos";??
public?static?void?main(String?args[])?throws?IOException?{??
//?創(chuàng)建目標(biāo)文件夾???
(new?File(url2)).mkdirs();??
//?獲取源文件夾當(dāng)前下的文件或目錄???
File[]?file?=?(new?File(url1)).listFiles();??
for?(int?i?=?0;?i??file.length;?i++)?{??
if?(file[i].isFile())?{??
//?復(fù)制文件???
copyFile(file[i],new?File(url2+file[i].getName()));??
}??
if?(file[i].isDirectory())?{??
//?復(fù)制目錄???
String?sourceDir=url1+File.separator+file[i].getName();??
String?targetDir=url2+File.separator+file[i].getName();??
copyDirectiory(sourceDir,?targetDir);??
}??
}??
}??
//?復(fù)制文件???
public?static?void?copyFile(File?sourceFile,File?targetFile)???
throws?IOException{??
//?新建文件輸入流并對(duì)它進(jìn)行緩沖???
FileInputStream?input?=?new?FileInputStream(sourceFile);??
BufferedInputStream?inBuff=new?BufferedInputStream(input);??
//?新建文件輸出流并對(duì)它進(jìn)行緩沖???
FileOutputStream?output?=?new?FileOutputStream(targetFile);??
BufferedOutputStream?outBuff=new?BufferedOutputStream(output);??
//?緩沖數(shù)組???
byte[]?b?=?new?byte[1024?*?5];??
int?len;??
while?((len?=inBuff.read(b))?!=?-1)?{??
outBuff.write(b,?0,?len);??
}??
//?刷新此緩沖的輸出流???
outBuff.flush();??
//關(guān)閉流???
inBuff.close();??
outBuff.close();??
output.close();??
input.close();??
}??
//?復(fù)制文件夾???
public?static?void?copyDirectiory(String?sourceDir,?String?targetDir)??
throws?IOException?{??
//?新建目標(biāo)目錄???
(new?File(targetDir)).mkdirs();??
//?獲取源文件夾當(dāng)前下的文件或目錄???
File[]?file?=?(new?File(sourceDir)).listFiles();??
for?(int?i?=?0;?i??file.length;?i++)?{??
if?(file[i].isFile())?{??
//?源文件???
File?sourceFile=file[i];??
//?目標(biāo)文件???
File?targetFile=new?File(new?File(targetDir).getAbsolutePath()+File.separator+file[i].getName());??
copyFile(sourceFile,targetFile);??
}??
if?(file[i].isDirectory())?{??
//?準(zhǔn)備復(fù)制的源文件夾???
String?dir1=sourceDir?+?"/"?+?file[i].getName();??
//?準(zhǔn)備復(fù)制的目標(biāo)文件夾???
String?dir2=targetDir?+?"/"+?file[i].getName();??
copyDirectiory(dir1,?dir2);??
}??
}??
}??
}
一個(gè)簡(jiǎn)單的方式就是調(diào)用cmd命令,使用windows自帶的功能來(lái)替你完成這個(gè)功能
我給你寫個(gè)例子
import java.io.*;
public class test{
public static void main(String[] args){
BufferedReader in = null;
try{
// 這里你就當(dāng)作操作對(duì)dos一樣好了 不過(guò)cmd /c 一定不要?jiǎng)?/p>
Process pro = Runtime.getRuntime().exec("cmd /c copy d:\\ReadMe.txt e:\\");
in = new BufferedReader(new InputStreamReader(pro.getInputStream()));
String str;
while((str = in.readLine()) != null){
System.out.println(str);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(in != null){
try{
in.close();
}catch(IOException i){
i.printStackTrace();
}
}
}
}
}
使用Java語(yǔ)言如何實(shí)現(xiàn)快速文件復(fù)制:
代碼:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class Test {
public static void main(String[] args){
long start = System.currentTimeMillis();
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
FileChannel inFileChannel = null;
FileChannel outFileChannel = null;
try {
fileInputStream = new FileInputStream(new File("C:\\from\\不是鬧著玩的.flv"));
fileOutputStream = new FileOutputStream(new File("C:\\to\\不是鬧著玩的.flv"));
inFileChannel = fileInputStream.getChannel();
outFileChannel = fileOutputStream.getChannel();
inFileChannel.transferTo(0, inFileChannel.size(), outFileChannel);//連接兩個(gè)通道,從in通道讀取數(shù)據(jù)寫入out通道。
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fileInputStream != null){
fileInputStream.close();
}
if(inFileChannel != null){
inFileChannel.close();
}
if(fileOutputStream != null){
fileOutputStream.close();
}
if(outFileChannel != null){
outFileChannel.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println("視頻文件從“from”文件夾復(fù)制到“to”文件需要" + (end - start) + "毫秒。");
}
}