递归与非递归操作目录
最近在做个两台机器通过socket传目录的功能,在传的过程中需要展示进度条,所以进度的展示总需要先统计文件数或文件的大小才能展现,递归的实现就不能用了。
后来是通过先把目录下所有的文件和文件夹列一遍,放到两个List中,另一端按照相应的结构创建目录,再接收传来的socket。
所以顺手又写了写递归与非递归处理目录的代码。
public void recursiveDeleteDir(String dir) {File f = new File(dir);if(f.isFile()) {f.delete();return;}File[] files = f.listFiles();for(int i=0; i< files.length; i++) {if(files[i].isDirectory()) {if(files[i].list().length == 0) {files[i].delete();}else {recursiveDeleteDir(files[i].getAbsolutePath());}} else {files[i].delete();}}}public void nonRecursiveDeleteDir(String dir) {File f = new File(dir);if(f.isFile()) {f.delete();return;}LinkedList<File> list = new LinkedList<File>();list.add(f);while(list.size() > 0 ) {File temp = list.removeFirst();File[] files = temp.listFiles();for(int i=0;i < files.length; i++) {if(files[i].isDirectory()) {if(files[i].listFiles().length == 0) {files[i].delete();}else {list.add(files[i]);}}else {files[i].delete();}}}}///////////////////以下为递归拷贝//////////////////////////////public void recursiveCopyDir(String src, String dest) {File f = new File(src);String[] files = f.list();for(int i=0; i<files.length; i++) {File temp = new File(src,files[i]);if(temp.isDirectory()) {File t = new File(dest,files[i]);if(!t.exists()) {t.mkdirs();}recursiveCopyDir(temp.getAbsolutePath(),new File(dest,files[i]).getAbsolutePath());}else {try {FileInputStream fis = new FileInputStream(temp);FileOutputStream fos = new FileOutputStream(new File(dest,files[i]));byte[] b = new byte[1024];int x = -1;while( (x = fis.read(b)) != -1) {fos.write(b);}fos.flush();fis.close();fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}}public void nonRecursiveCopyDir(String src, String dest) {File f = new File(src);LinkedList<File> list = new LinkedList<File>();list.add(new File(src));while(list.size() > 0) {File removed = list.removeFirst();File[] fik = removed.listFiles();for(int i=0; i< fik.length; i++) {String subpath = fik[i].getAbsolutePath().substring(src.length());File destFile = new File(dest +subpath);if(fik[i].isDirectory()) {if(!destFile.exists()) {destFile.mkdirs();}list.add(fik[i]);}else {try {FileInputStream fis = new FileInputStream(fik[i]);FileOutputStream fos = new FileOutputStream(destFile);byte[] b = new byte[1024];int x = -1;while( (x = fis.read(b)) != -1) {fos.write(b);}fos.flush();fis.close();fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}}String[] files = f.list();for(int i=0; i<files.length; i++) {File temp = new File(src,files[i]);if(temp.isDirectory()) {File t = new File(dest,files[i]);if(!t.exists()) {t.mkdirs();}recursiveCopyDir(temp.getAbsolutePath(),new File(dest,files[i]).getAbsolutePath());}else {}}}////////////////////////////////以下用于递归和非递归显示目录内容/////////////////////public void recursiveDir(String dir) {File f = new File(dir);File[] files = f.listFiles();for(File x : files) {if (x.isDirectory()) {recursiveDir(x.getAbsolutePath());} else {System.out.println(x.getAbsolutePath());}}}public void nonRecursiveDir(String dir) {File f = new File(dir);LinkedList<File> list = new LinkedList<File>();list.add(f);while(list.size() > 0) {File file = list.removeFirst();File[] files = file.listFiles();for(File x : files) {if (x.isDirectory()) {list.add(x);} else {System.out.println(x.getAbsolutePath());}}}}