com.attilax.zip.FileUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.attilax.zip.FileUtil.java

Source

/*
 * 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 com.attilax.zip;

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;
import com.attilax.io.filex;

/**
 * 
 * 
 * @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();
        return "";
    }

    /**
     * ?
     * 
     * @param dir            ??
     * @return fileList    
     */
    public static File[] ls(String dir) {
        return new File(dir).listFiles();
    }

    /**
     * ??
     * 
     * @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;
        }
    }

    // ?FileUtil
    /**
        
     *  ??
        
     * 
        
     * @param fileName
        
     *            ?? 
        
     * @param isDirectory ?
        
     * @return
        
     * @author yayagepei
        
     * @date 2008-8-27
        
     */

    public static File buildFile(String fileName, boolean isDirectory) {

        File target = new File(fileName);

        if (isDirectory) {

            target.mkdirs();

        } else {

            if (!target.getParentFile().exists()) {

                target.getParentFile().mkdirs();

                target = new File(target.getAbsolutePath());

            }

        }

        return target;

    }
}