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

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

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

    java Source

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      14 Posts :: 24 Stories :: 8 Comments :: 0 Trackbacks
    /*
     * FileUtil.java
     * Copyright (C) 2007-3-19  <JustinLei@gmail.com>
     *
     *        This program is free software; you can redistribute it and/or modify
     *        it under the terms of the GNU General Public License as published by
     *      the Free Software Foundation; either version 2 of the License, or
     *     (at your option) any later version.
     *
     *       This program is distributed in the hope that it will be useful,
     *      but WITHOUT ANY WARRANTY; without even the implied warranty of
     *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *        GNU General Public License for more details.
     *
     
    */
    package org.lambdasoft.utils;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.InputStream;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Properties;
    import java.util.Random;
    import java.util.Set;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;

    /**
     * 文件工具類
     * 
     * 
    @author TangLei <justinlei@gmail.com>
     * @date 2009-2-24
     
    */
    public class FileUtil {
        
    private static Log log = LogFactory.getLog(FileUtil.class);
        
    private FileUtil() {}
        
        
    /**
         * 獲取隨機的文件名稱
         * 
    @param seed    隨機種子
         * 
    @return
         
    */
        
    public static String getRandomFileName(String seed) {
            
    byte[] ra = new byte[100];
            
    new Random().nextBytes(ra);
            StringBuilder build 
    = new StringBuilder("");
            
    for (int i = 0; i < ra.length; i++) {
                build.append(Byte.valueOf(ra[i]).toString());
            }
            String currentDate 
    = Long.valueOf(new Date().getTime()).toString();
            seed 
    = seed + currentDate + build.toString();
            
    return EncryptUtils.getMD5ofStr(seed).toLowerCase();
        }
        
        
    /**
         * 列出所有當前層的文件和目錄
         * 
         * 
    @param dir            目錄名稱
         * 
    @return fileList    列出的文件和目錄
         
    */
        
    public static File[] ls(String dir) {
            
    return new File(dir).listFiles();
        }
        
        
    /**
         * 根據(jù)需要創(chuàng)建文件夾
         * 
         * 
    @param dirPath 文件夾路徑
         * 
    @param del    存在文件夾是否刪除
         
    */
        
    public static void mkdir(String dirPath,boolean del) {
            File dir 
    = new File(dirPath);
            
    if(dir.exists()) {
                
    if(del)
                    dir.delete();
                
    else return;
            }
            dir.mkdirs();
        }
        
        
    /**
         * 刪除文件和目錄
         * 
         * 
    @param path
         * 
    @throws Exception
         
    */
        
    public static void rm(String path) throws Exception{
            
    if(log.isDebugEnabled())
                log.debug(
    "需要刪除的文件: " + path);
            File file 
    = new File(path);
            
    if(!file.exists()) {
                
    if(log.isWarnEnabled())
                    log.warn(
    "文件<" + path + ">不存在");
                
    return;
            }
            
    if(file.isDirectory()) {
                File[] fileList 
    = file.listFiles();
                
    if(fileList == null || fileList.length == 0) {
                    file.delete();
                } 
    else {
                    
    for (File _file : fileList) {
                        rm(_file.getAbsolutePath());
                    }
                }
            file.delete();
            } 
    else {
                file.delete();
            }
        }
        
        
    /**
         * 移動文件
         * 
         * 
    @param source     源文件
         * 
    @param target         目標文件
         * 
    @param cache        文件緩存大小
         * 
    @throws Exception
         
    */
        
    public static void mv(String source,String target,int cache) throws Exception {
            
    if(source.trim().equals(target.trim()))
                
    return;
            
    byte[] cached = new byte[cache];
            FileInputStream fromFile 
    = new FileInputStream(source);
            FileOutputStream toFile 
    = new FileOutputStream(target);
            
    while(fromFile.read(cached) != -1) {
                toFile.write(cached);
            }
            toFile.flush();
            toFile.close();
            fromFile.close();
            
    new File(source).deleteOnExit();
        }
        
        
    /**
         * 把屬性文件轉換成Map
         * 
         * 
    @param propertiesFile
         * 
    @return
         * 
    @throws Exception
         
    */
        
    public static final Map<String, String> getPropertiesMap(String propertiesFile) throws Exception{
            Properties properties 
    = new Properties();
            FileInputStream inputStream 
    = new FileInputStream(propertiesFile);
            properties.load(inputStream);
            Map
    <String, String> map = new HashMap<String, String>();
            Set
    <Object> keySet = properties.keySet();
            
    for (Object key : keySet) {
                map.put((String)key, properties.getProperty((String)key));
            }
            
    return map;
        }
        
        @SuppressWarnings(
    "unchecked")
        
    public static final Map<String, String> getPropertiesMap(Class clazz,String fileName) throws Exception{
            Properties properties 
    = new Properties();
            InputStream inputStream 
    = clazz.getResourceAsStream(fileName);
            
    if(inputStream == null)
                inputStream 
    = clazz.getClassLoader().getResourceAsStream(fileName);
            properties.load(inputStream);
            Map
    <String, String> map = new HashMap<String, String>();
            Set
    <Object> keySet = properties.keySet();
            
    for (Object key : keySet) {
                map.put((String)key, properties.getProperty((String)key));
            }
            
    return map;
        }
        
        
    /**
         * 把屬性文件轉換成Map
         * 
         * 
    @param inputStream
         * 
    @return
         * 
    @throws Exception
         
    */
        
    public static final Map<String, String> getPropertiesMap(InputStream inputStream) throws Exception{
            Properties properties 
    = new Properties();
            properties.load(inputStream);
            Map
    <String, String> map = new HashMap<String, String>();
            Set
    <Object> keySet = properties.keySet();
            
    for (Object key : keySet) {
                map.put((String)key, properties.getProperty((String)key));
            }
            
    return map;
        }
        
        
    /**
         * 把文本文件轉換成String
         * 
         * 
    @param fullPath
         * 
    @return
         * 
    @throws Exception
         
    */
        
    public static String readFile(String fullPath) throws Exception{
            BufferedReader reader 
    = new BufferedReader(new FileReader(fullPath));
            
    if(reader == null)
                
    return null;
            StringBuilder builder 
    = new StringBuilder("");
            String line 
    = null;
            
    while((line = reader.readLine()) != null) {
                builder.append(line 
    + "\n");
            }
            
    return builder.toString();
        }
        
        
    /**
         * 獲取資源文件流
         * 
         * 
    @param clazz
         * 
    @param name
         * 
    @return
         
    */
        @SuppressWarnings(
    "unchecked")
        
    public static InputStream getResourceAsStream(Class clazz,String name) {
            
    try {
                InputStream inputStream 
    = clazz.getResourceAsStream(name);
                
    if(inputStream == null
                    inputStream 
    = clazz.getClassLoader().getResourceAsStream(name);
                
    return inputStream;
            } 
    catch (Exception e) {
                
    if(log.isWarnEnabled())
                    log.warn(
    "獲取資源文件失敗", e);
                
    return null;
            }
        }
    }
    posted on 2009-12-18 16:40 JustinLei 閱讀(2312) 評論(1)  編輯  收藏

    Feedback

    # re: 文件工具類FileUtil 2009-12-19 11:09 marten
    Good  回復  更多評論
      


    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導航:
     
    主站蜘蛛池模板: caoporm超免费公开视频| 亚洲精品成a人在线观看☆| 久久国产免费直播| 亚洲色偷拍区另类无码专区| 免费国产黄网站在线看| 免费日本黄色网址| 美女视频黄a视频全免费网站一区 美女视频黄a视频全免费网站色 | 国产午夜鲁丝片AV无码免费| 亚洲中文无码永久免| 成年女人毛片免费播放人| 国产成人亚洲精品| 大学生高清一级毛片免费| 亚洲av最新在线观看网址| 亚洲国产精品自产在线播放| 一级一级一级毛片免费毛片| 亚洲国产无套无码av电影| 日韩免费人妻AV无码专区蜜桃 | 国产精品视频免费一区二区三区| 国产亚洲精品美女| 亚洲国产日韩一区高清在线| 久久伊人免费视频| 亚洲91精品麻豆国产系列在线| 最近最好的中文字幕2019免费| 亚洲av日韩专区在线观看| 国产一精品一aⅴ一免费| www一区二区www免费| 亚洲综合精品一二三区在线 | 69天堂人成无码麻豆免费视频| 中文字幕亚洲精品无码| 国产午夜无码视频免费网站| 嫩草在线视频www免费看| 亚洲国产精品线观看不卡| 国产男女猛烈无遮挡免费视频网站| 一区二区视频在线免费观看| 亚洲国产精品久久久久婷婷老年| 成人免费午夜在线观看| 在线播放免费人成视频网站| 永久免费视频v片www| WWW免费视频在线观看播放| 亚洲三级中文字幕| 亚洲精品成a人在线观看|