Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2561208
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-12-29 11:28:48

本文主要记录了使用java创建目录,复制目录,遍历目录及文件,删除目录等。

Java comes with two create directory methods :
 1. File(‘directory’).mkdir()
new File("C:\\Directory1").mkdir();
Create a directory named “Directory1″
 2. File(‘directories’).mkdirs()

new File("C:\\Directory2\\Sub2\\Sub-Sub2").mkdirs()
Create a directory named “Directory2 and all its sub-directories “Sub2″ and “Sub-Sub2″ in one shot.


To delete a directory, you can simply use the File.delete(), but the directory must be empty in order to delete it.

Often times, you may require to perform recursive delete in a directory, which means all it’s sub-directories and files should be delete as well.

完整代码如下:

  1. package org.hnrsc.directory;

  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;

  8. public class JavaDirectory {

  9.     public static void main(String[] args){
  10.         System.out.println("------------create directory--------------");
  11.         //create directory
  12.         File file = new File("C:\\folder1");
  13.         if(file.mkdir()){
  14.             System.out.println("Directory is created.");
  15.         }else{
  16.             System.out.println("Failed to create directory.");
  17.         }
  18.         
  19.         //create directories
  20.         File files = new File("C:\\folder2\\folder2-1\\folder2-3");
  21.         if(files.mkdirs()){
  22.             System.out.println("Multiple directories are created");
  23.         }else{
  24.             System.out.println("Failed to create multiple directories.");
  25.         }
  26.         
  27.         System.out.println("-----------copy folder---------------");
  28.         File srcFolder = new File("C:\\logs");
  29.         File destFolder = new File("C:\\logs-bak");
  30.         
  31.         //make sure source folder exists
  32.         if(!srcFolder.exists()){
  33.             System.out.println("Directory does not exists.");
  34.             System.exit(0);
  35.         }else{
  36.             try{
  37.                 copyFolder(srcFolder,destFolder);
  38.             }catch(IOException e){
  39.                 e.printStackTrace();
  40.                 System.exit(0);
  41.             }
  42.         }
  43.         System.out.println("-----------traverse directory---------------");
  44.         traverseDir(new File("C:\\logs-bak"));
  45.         System.out.println("-----------delete node--------------");
  46.         deleteNode(new File("C:\\logs-bak"));
  47.     }

  48.     private static void copyFolder(File src, File dest) throws IOException {
  49.         
  50.         if(src.isDirectory()){
  51.             //if diirectory not exists ,create it
  52.             if(!dest.exists()){
  53.                 dest.mkdir();
  54.                 System.out.println("Directory copied from " + src +" to " + dest);
  55.             }
  56.             //list all the directory contents
  57.             String[] files = src.list();
  58.             
  59.             for(String file :files){
  60.                 //construct the src and dest file structure
  61.                 File srcFile = new File(src,file);
  62.                 File destFile = new File(dest,file);
  63.                 
  64.                 copyFolder(srcFile,destFile);
  65.             }
  66.         }else{
  67.             //if file, then copy it use bytes stream to support all file types
  68.             InputStream in = new FileInputStream(src);
  69.             OutputStream out = new FileOutputStream(dest);
  70.             
  71.             byte[] buffer = new byte[1024];
  72.             int len;
  73.             while((len = in.read(buffer)) > 0){
  74.                 out.write(buffer,0,len);
  75.             }
  76.             in.close();
  77.             out.close();
  78.         }
  79.     }
  80.     
  81.     public static void traverseDir(File node){
  82.         System.out.println(node.getAbsoluteFile());
  83.         
  84.         if(node.isDirectory()){
  85.             String[] subnode = node.list();
  86.             for(String filename : subnode){
  87.                 traverseDir(new File(node,filename));
  88.             }
  89.         }
  90.     }
  91.     
  92.     public static void deleteNode(File node){
  93.         
  94.         if(node.isDirectory()){
  95.             
  96.             //directory is empty, then delete it
  97.             if(node.list().length == 0){
  98.                 node.delete();
  99.                 System.out.println("Directory is deleted: "+ node.getAbsolutePath());
  100.             }else{
  101.                 //list all the directory contents
  102.                 String files[] = node.list();
  103.                 for(String temp:files){
  104.                     //construct the file structure
  105.                     File fileDelete = new File(node,temp);
  106.                     
  107.                     //recursive delete
  108.                     deleteNode(fileDelete);
  109.                 }
  110.                 
  111.                 //check the dir again , if empty then delete
  112.                 if(node.list().length == 0){
  113.                     node.delete();
  114.                     System.out.println("Directory is deleted: "+ node.getAbsolutePath());
  115.                 }
  116.             }
  117.         }else{
  118.             //if file then delete it
  119.             node.delete();
  120.             System.out.println("File is deleted : " + node.getAbsolutePath());
  121.         }
  122.     }
  123. }

阅读(2799) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~