复制一个文件夹中的所有文件和文件夹的java程序实现
使用java程序?qū)崿F(xiàn)了對文件夾的復制功能:
?package demo.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Date;
public class TestIo {
?public static float length = 0;?
?s?public ?void isFile(File f ,String dir) throws IOException{ //使用遞歸的方法遍歷該目錄下的所有文件和文件夾
??File[] farry = f.listFiles(); //得到該文件夾下的所有目錄和文件
??for(int i=0;i<farry.length;i++){
???String fileName = farry[i].getName();
???String filePath = farry[i].getPath();
???String path = changPath(filePath);?? //將路徑中的\替換為/
???String newPath = newPath(path, dir);?? //建立目標文件路徑
???if(farry[i].isDirectory()){? //判斷是否是目錄文件
????boolean b = createFile(newPath);? //創(chuàng)建與該文件夾同名的目標文件中的文件夾 創(chuàng)建成功返回true失敗返回false
????System.out.println(fileName);
????? ?System.out.println(filePath);
????? ?System.out.println(newPath);
????? ?System.out.println(b);
????System.out.println("目錄名:"+farry[i].getName() +" : 路徑: "+farry[i].getPath());? //打印該目錄的名稱和路徑
????isFile(farry[i],dir);??????? //遞歸調(diào)用該方法
???}else{? //如果不是目錄文件復制該文件到指定的路徑上
????copyFile(path, newPath); //將該文件復制到目標文件夾中
????totalLength(farry[i].length()/1024);
????System.out.println("文件名:"+farry[i].getName()+" :路徑: "+farry[i].getPath()+" : 文件大小:"+farry[i].length()/1024+"KB");
???}
???
???
??}
??
?}
?
?public String changPath(String path){? //修改路徑
??path = path.replace("\\","/");
??return path;
?}
?public String newPath(String path,String dir){? //創(chuàng)建新的目錄地址
??String newPath = path.substring(path.indexOf(":")+1);
??newPath = dir+newPath;
??return newPath;
?}
?public boolean createFile(String path) throws IOException{?? //創(chuàng)建文件夾
??File f = new File(path);
??boolean flag = false;
??//?f.createNewFile();
??flag = ?f.mkdir();
??if(f.isDirectory()){
???flag = true;
??}
??return flag;
?}
?public void copyFile(String inPath,String outPath){ //復制文件夾中的文件到目標文件夾
??try {
???BufferedReader br = new BufferedReader(new FileReader(inPath));
???BufferedWriter bw = new BufferedWriter(new FileWriter(outPath));
???String len = br.readLine();
???while (len != null) {
????bw.write(len);
????len = br.readLine();
???}
???br.close();? //關閉輸入流
???bw.close();? //關閉輸出流
??} catch (FileNotFoundException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??} catch (IOException e) {
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
?}
?public void firstCopy(File f, String dir){? //在復制之前建立目標文件夾中的根目錄 如果該文件不是文件夾則直接復制該文件
??String path = f.getPath();
??path = changPath(path);
??String newPath = newPath(path, dir);
??if(f.isDirectory()){
???try {
????boolean flag = createFile(newPath);
????isFile(f,dir); //遍歷該文件夾
???} catch (IOException e) {
????// TODO Auto-generated catch block
????System.out.println("路徑不正確");
????e.printStackTrace();
???}
??}else{ //不是文件夾則直接復制該文件
???copyFile(path, newPath);
??}
??
?}
?public float totalLength(float f){ //計算該文件夾中所有文件的總長度
??length = length+f;
??return length;
?};
?public static void main(String[] args) throws IOException {
?
??TestIo t = new TestIo();
??Date date = new Date();
??long start = 0;
??long end = 0;
??start = date.getTime();
???? File f = new File("F:/11");? //需要拷貝的文件或文件夾路徑
???? t.firstCopy(f, "E:/22");???????? //目標路徑
???? Date d2 = new Date();
???? end = d2.getTime();
???? System.err.println("總用時為:"+(end-start));
???? System.err.println("復制文件的總大小為:"+length/1024+" MB");
??? // t.doCopy2();
?}
}
遍歷是使用遞歸實現(xiàn)
注釋寫的挺詳細的,相信大家直接看注釋也看以看的差不多,如果有什么疑問可以隨時提出,方便大家一起交流。
轉載于:https://www.cnblogs.com/vTree/archive/2011/08/12/2136157.html
總結
以上是生活随笔為你收集整理的复制一个文件夹中的所有文件和文件夹的java程序实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高级SQL注入拿shell,一般黑客不知
- 下一篇: android 学习查阅笔记