<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    J2EE社區(qū)

    茍有恒,何必三更起五更眠;
    最無益,只怕一日曝十日寒.
    posts - 241, comments - 318, trackbacks - 0, articles - 16

     

    package com.wepull.demo;

     

    import java.io.*;

    import java.util.zip.*;

     

    /**

     * 對(duì)文件或者目錄操作的類

     * 
    @version 1.0

     * 
    @author leno

     
    */


    public class FileUtil {

     

       
    private static void copy(File source, File target) throws IOException {

          File tar 
    = new File(target, source.getName());

          
    if (source.isDirectory()) {

            System.out.println(
    "開始創(chuàng)建目錄:" + tar.getPath());

            tar.mkdir();

            File[] fs 
    = source.listFiles();

            
    for (int i = 0; i < fs.length; i++{

               copy(fs[i], tar);

            }


          }
     else {

            System.out.println(
    "開始從" + source + "拷貝文件到" + tar.getPath());

            InputStream is 
    = new FileInputStream(source);

            OutputStream os 
    = new FileOutputStream(tar);

            
    byte[] buf = new byte[1024];

            
    int len = 0;

            
    while ((len = is.read(buf)) != -1{

               os.write(buf, 
    0, len);

            }


            is.close();

            os.close();

          }


       }


     

       
    /**

        * 拷貝文件或者目錄到某個(gè)指定的路徑

        * 

        * 
    @param source

        *            源文件或者目錄

        * 
    @param target

        *            目標(biāo)路徑

        * 
    @throws IOException

        
    */


       
    public static void copy(String source, String target) {

          File sour 
    = new File(source);

          File tar 
    = new File(target);

          
    try {

            copy(sour, tar);

          }
     catch (IOException e) {

            
    // TODO Auto-generated catch block

            e.printStackTrace();

          }


       }


     

       
    private static void delete(File file) {

          
    if (file.isDirectory()) {

            File[] fs 
    = file.listFiles();

            
    for (int i = 0; i < fs.length; i++{

               delete(fs[i]);

            }


            file.delete();

          }
     else {

            file.delete();

          }


       }


     

       
    /**

        * 刪除一個(gè)文件或者目錄

        * 

        * 
    @param file

        
    */


       
    public static void delete(String path) {

          File file 
    = new File(path);

          delete(file);

       }


     

       
    /**

        * 壓縮文件或者目錄到指定的路徑

        * 

        * 
    @param zipFileName

        *            目標(biāo)路徑

        * 
    @param inputPath

        *            被壓縮的文件或者目錄

        
    */


       
    public static void zip(String zipFileName, String inputPath) {

          File inputFile 
    = new File(inputPath);

          ZipOutputStream out;

          
    try {

            out 
    = new ZipOutputStream(new FileOutputStream(zipFileName));

            zip(out, inputFile, inputFile.getName());

            System.out.println(
    "壓縮完成!");

            out.close();

          }
     catch (Exception e) {

            
    // TODO Auto-generated catch block

            e.printStackTrace();

          }


       }


     

       
    private static void zip(ZipOutputStream out, File f, String base)

            
    throws Exception {

          System.out.println(
    "正在壓縮:" + f.getName() + " ");

          
    if (f.isDirectory()) {

            File[] fs 
    = f.listFiles();

            base 
    += "/";

            System.out.println(
    "新建目錄條目:" + f.getName());

            out.putNextEntry(
    new ZipEntry(base)); // 生成相應(yīng)的目錄

            
    for (int i = 0; i < fs.length; i++{

               
    // 對(duì)本目錄下的所有文件對(duì)象遞歸調(diào)用本方法

               zip(out, fs[i], base 
    + fs[i].getName());

            }


          }
     else {

            System.out.println(
    "新增文件條目:" + f.getName());

            out.putNextEntry(
    new ZipEntry(base));

            InputStream is 
    = new FileInputStream(f);

            
    byte[] buf = new byte[1024];

            
    int len = 0;

            
    while ((len = is.read(buf)) != -1{

               out.write(buf, 
    0, len);

            }


            is.close();

          }


       }


     

       
    /**

        * 解壓縮zip文件到指定的路徑

        * 

        * 
    @param zipfile

        *            zip格式壓縮文件

        * 
    @param desPath

        *            目標(biāo)路徑

        
    */


       
    public static void unzip(String zipFile, String desPath) {

          
    // 建立輸出流,用于將從壓縮文件中讀出的文件流寫入到磁盤

          OutputStream out 
    = null;

          
    // 建立輸入流,用于從壓縮文件中讀出文件

          ZipInputStream is;

          
    try {

            is 
    = new ZipInputStream(new FileInputStream(zipFile));

            ZipEntry entry 
    = null;

            
    while ((entry = is.getNextEntry()) != null{

               System.out.println(
    "正在解壓縮:" + entry.getName() + " ");

               File f 
    = new File(desPath + "\\" + entry.getName());

               
    if (entry.isDirectory()) {

                  System.out.println(
    "新建目錄:" + f.getName());

                  f.mkdir();

               }
     else {

                  System.out.println(
    "新增文件:" + f.getName());

                  
    // 根據(jù)壓縮文件中讀出的文件名稱新建文件

                  out 
    = new FileOutputStream(f);

                  
    byte[] buf = new byte[1024];

                  
    int len = 0;

                  
    while ((len = is.read(buf)) != -1{

                     out.write(buf, 
    0, len);

                  }


                  out.close();

               }


            }


            is.close();

          }
     catch (Exception e) {

            
    // TODO Auto-generated catch block

            e.printStackTrace();

          }


       }


     

       
    /**

        * 創(chuàng)建新文件

        * 

        * 
    @param path

        
    */


       
    public static void createFile(String path) {

          File file 
    = new File(path);

          
    try {

            file.createNewFile();

          }
     catch (IOException e) {

            
    // TODO Auto-generated catch block

            e.printStackTrace();

          }


     

       }


     

       
    /**

        * 創(chuàng)建新目錄

        * 

        * 
    @param path

        
    */


       
    public static void createDir(String path) {

          File file 
    = new File(path);

          file.mkdirs();

       }


     

       
    /**

        * 剪切文件或者目錄到某個(gè)指定的路徑

        * 

        * 
    @param source

        *            源文件或者目錄

        * 
    @param target

        *            目標(biāo)路徑

        * 

        
    */


       
    public static void cutTo(String source, String target) {

          File sourFile 
    = new File(source);

          File tarFile 
    = new File(target);

          
    if (sourFile.isFile()) {

            
    if (tarFile.isDirectory()) {

               sourFile.renameTo(tarFile);

            }


          }
     else {

            copy(source, target);

            delete(source);

          }


       }


     

       
    public static void main(String[] args) {

          
    // copy("E:\\w.txt", "E:\\a");

          
    // delete("E:\\a");

          
    // zip("E:\\a.zip", "E:\\b");

          
    // unzip("E:\\a.zip", "E:\\b");

          
    // createFile("E:\\a.txt");

          
    // createDir("E:\\bb");

          
    // cutTo("E:\\b", "D:\\");

       }


    }
    本文來自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/lenotang/archive/2008/07/23/2698562.aspx



    名稱: ?4C.ESL | .↗Evon
    口號(hào): 遇到新問題?先要尋找一個(gè)方案乄而不是創(chuàng)造一個(gè)方案こ
    mail: 聯(lián)系我


    主站蜘蛛池模板: 两个人日本WWW免费版| 猫咪免费人成网站在线观看| 亚洲动漫精品无码av天堂| 亚洲免费一级视频| 亚洲综合欧美色五月俺也去| 亚洲AV无码之日韩精品| 日本在线免费播放| 亚洲AV无码一区二区三区鸳鸯影院 | 亚洲AV色无码乱码在线观看| 亚洲乱码日产精品a级毛片久久| 免费毛片a线观看| 亚洲乱亚洲乱妇无码| 亚洲av日韩av天堂影片精品| 国内外成人免费视频| a毛看片免费观看视频| 亚洲老熟女五十路老熟女bbw | 久久亚洲国产视频| 国产精品深夜福利免费观看| 久久精品免费电影| 污污视频网站免费观看| 亚洲黄色中文字幕| 毛茸茸bbw亚洲人| 亚洲成在人线aⅴ免费毛片| 国产午夜精品理论片免费观看| 亚洲成AV人片在WWW| 激情内射亚洲一区二区三区| 亚洲人成无码www久久久| 国产va精品免费观看| 男人进去女人爽免费视频国产| 成人精品国产亚洲欧洲| 亚洲成电影在线观看青青| 中文字幕精品亚洲无线码一区| 丁香花免费高清视频完整版| 国产一区二区免费视频| 国产精品青草视频免费播放| 亚洲av永久无码精品网址| 亚洲免费在线视频播放| 亚洲A∨无码无在线观看| 久久亚洲国产成人影院网站| 四虎免费大片aⅴ入口| 91免费人成网站在线观看18|