JAVA

人生若只如初见,何事秋风悲画扇。

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  50 随笔 :: 25 文章 :: 157 评论 :: 0 Trackbacks

经常对一些文件,文件夹操作,自己写一个不是难事.但有个可以捡的应该也不是坏事.
将destFileName解压到mExtractToDir  目录:

public   void  extract(destFileName,mExtractToDir ) 
 

  String fileName 
=   new  String(); 
  String destFileName 
=   new  String(); 
  
  
// do our own buffering; reuse the same 
  buffer  byte [] buffer  =   new   byte [ 16384 ]; 
  
try  
  

   ZipFile archive 
=   new  ZipFile( mZipFile ); 
   
for  ( Enumeration e  =  archive.entries(); e.hasMoreElements(); ) 
   

    
// get the next entry in the archive 
    ZipEntry entry  =  (ZipEntry)e.nextElement(); 
    
    
if  (  !  entry.isDirectory() ) 
    

     fileName 
=  entry.getName(); 
     fileName 
=  fileName.replace( ' / ' , File.separatorChar); 
     
     destFileName 
=  mExtractToDir  +  fileName; 
     File destFile 
=   new  File(destFileName);
     
     
// create the destination path, if needed     
     String parent  =  destFile.getParent(); 
     
if  ( parent  !=   null  ) 
     

      File parentFile 
=   new  File(parent); 
      
if  (  !  parentFile.exists() ) 
      

       
// create the chain of subdirs to the file 
       parentFile.mkdirs(); 
      }
 
     }
 
     
     
// get a stream of the archive entry's bytes 
     InputStream in  =  archive.getInputStream(entry); 
     
     
// open a stream to the destination 
     file OutputStream out  =   new  FileOutputStream(destFileName); 
     
     
// Repeat reading into buffer and writing buffer to file, 
     
// until done. Count will always be # bytes read, until  // EOF when it is -1. 
      int  count; 
     
while  ( (count  =  in.read(buffer))  !=   - 1  ) 
     

      out.write(buffer, 
0 , count ); 
     }
 in.close(); out.close(); 
     }
 
    }
 
   }
 
  
catch ( ZipException ze ) 
  

   ze.printStackTrace(); 
  }
 
  
catch  ( NullPointerException npe ) 
  

   npe.printStackTrace(); 
  }
 
  
catch  ( IOException ioe ) 
  

   ioe.printStackTrace(); 
  }
 
  
catch  ( SecurityException se )
  

   se.printStackTrace(); 
  }
 
 }

 

将某一文件夹的内容清空:

public   void  cleanImportDirectory(String iPath) 
 

  
try  
  

   File theFile 
=   new  File(iPath); 
   File allFiles[] 
=  theFile.listFiles(); 
   
   
for  ( int  i  =   0 ; i  <  allFiles.length; i ++
   

    
if (allFiles[i].isDirectory()) 
    

     cleanImportDirectory(allFiles[i].toString()); 
     allFiles[i].delete(); 
    }
 
    
else
    

     allFiles[i].delete(); 
    }
 
   }
 
  }
 
  
catch  (NullPointerException npe) 
  

   mLogger.severe(iPath 
+   "  did not exist and was not cleaned!! " ); 
  }
 
 }
 

 

将某一文件夹的内容移到指定文件夹:

private   void  copyCourse(String iInFilePath, String iOutFilePath)  throws  IOException 
 

  
try  
  

   String inDirName 
=  iInFilePath; 
   inDirName.replace(
' / ' , java.io.File.separatorChar); 
   
   File tempFile 
=   new  File(inDirName); 
   File[] fileNames 
=  tempFile.listFiles(); 
   
   String outDirName 
=  iOutFilePath; 
   outDirName 
=  outDirName.replace( ' / ' , java.io.File.separatorChar); 
   File tempDir 
=   new  File(outDirName); 
   tempDir.mkdirs(); 
   
for  ( int  i  =   0 ; i  <  fileNames.length; i ++
   

    String tempString 
=  outDirName  +  java.io.File.separatorChar  +  fileNames[i].getName(); 
    
if (fileNames[i].isDirectory()) 
    

     File dirToCreate 
=   new  File(tempString); 
     dirToCreate.mkdirs(); 
     copyCourse(fileNames[i].getAbsolutePath(), tempString); 
    }

    
else  
    

     BufferedInputStream in 
=   new  BufferedInputStream( new  FileInputStream(fileNames[i])); 
     BufferedOutputStream out 
=   new  BufferedOutputStream( new  FileOutputStream(tempString)); 
     
int  c; 
     
while ((c  =  in.read())  !=   - 1
     

      out.write(c); 
     }

     
     in.close(); 
     out.close(); 
    }
 
   }
 
  }
 
  
catch  (IOException IOE) 
  

    IOE.printStackTrace(); 
  }
 

我暂时只碰到了这三个...

posted on 2006-03-30 17:46 Jkallen 阅读(1277) 评论(1)  编辑  收藏 所属分类: JEE学习

评论

# re: 文件夹,压缩包操作 2006-04-28 10:06 小葱卷饼
大哥,有 压缩包最加方法吗?  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: