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

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

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

    J2EE社區

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

     

    package com.wepull.demo;

     

    import java.io.*;

    import java.util.zip.*;

     

    /**

     * 對文件或者目錄操作的類

     * 
    @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(
    "開始創建目錄:" + 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();

          }


       }


     

       
    /**

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

        * 

        * 
    @param source

        *            源文件或者目錄

        * 
    @param target

        *            目標路徑

        * 
    @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();

          }


       }


     

       
    /**

        * 刪除一個文件或者目錄

        * 

        * 
    @param file

        
    */


       
    public static void delete(String path) {

          File file 
    = new File(path);

          delete(file);

       }


     

       
    /**

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

        * 

        * 
    @param zipFileName

        *            目標路徑

        * 
    @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)); // 生成相應的目錄

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

               
    // 對本目錄下的所有文件對象遞歸調用本方法

               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

        *            目標路徑

        
    */


       
    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());

                  
    // 根據壓縮文件中讀出的文件名稱新建文件

                  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();

          }


       }


     

       
    /**

        * 創建新文件

        * 

        * 
    @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();

          }


     

       }


     

       
    /**

        * 創建新目錄

        * 

        * 
    @param path

        
    */


       
    public static void createDir(String path) {

          File file 
    = new File(path);

          file.mkdirs();

       }


     

       
    /**

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

        * 

        * 
    @param source

        *            源文件或者目錄

        * 
    @param target

        *            目標路徑

        * 

        
    */


       
    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博客,轉載請標明出處:http://blog.csdn.net/lenotang/archive/2008/07/23/2698562.aspx



    名稱: ?4C.ESL | .↗Evon
    口號: 遇到新問題?先要尋找一個方案乄而不是創造一個方案こ
    mail: 聯系我


    主站蜘蛛池模板: 亚洲综合激情五月丁香六月| 在线a级毛片免费视频| 亚洲区日韩精品中文字幕| 亚洲人色婷婷成人网站在线观看| 四虎www免费人成| 久草免费在线观看视频| 免费无码又爽又刺激网站直播| 美女露100%胸无遮挡免费观看| 亚洲精品日韩专区silk| 久久亚洲国产中v天仙www| 亚洲午夜无码片在线观看影院猛| 日韩免费无砖专区2020狼| 无码av免费毛片一区二区| 99re这里有免费视频精品| 今天免费中文字幕视频| 国产成人无码精品久久久免费| 色屁屁www影院免费观看视频| 亚洲欧美成aⅴ人在线观看| 亚洲国产情侣一区二区三区| 久久精品国产亚洲AV麻豆~ | 亚洲色www永久网站| 亚洲国产视频一区| 亚洲视频网站在线观看| 亚洲av女电影网| 亚洲成人激情在线| 久久精品国产亚洲av影院| 久久久久无码精品亚洲日韩| 久久亚洲精品中文字幕三区| 亚洲AV无码专区国产乱码4SE | 久久毛片免费看一区二区三区| 一级中文字幕免费乱码专区| 新最免费影视大全在线播放| 免费无码婬片aaa直播表情| 日本一区二区三区免费高清在线| 麻豆69堂免费视频| sss在线观看免费高清| 天黑黑影院在线观看视频高清免费 | 免费VA在线观看无码| 日本永久免费a∨在线视频| 一个人看的在线免费视频| 久99久无码精品视频免费播放|