asia.gkc.vneedu.utils.FileUtil.java Source code

Java tutorial

Introduction

Here is the source code for asia.gkc.vneedu.utils.FileUtil.java

Source

/*
 * Copyright 2016 Jakes Lee
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package asia.gkc.vneedu.utils;

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

import java.io.*;
import java.nio.channels.FileChannel;

/**
 * File Name: FileUtil.java
 * Function:
 *
 * @author jakes.
 * @version 1.0
 * @since 4/9/16 9:30 PM
 */

public class FileUtil {
    public static final Log logger = LogFactory.getLog(FileUtil.class);

    /**
     * 
     *
     * @param dir - 
     * @return 
     */
    public static boolean buildDir(String dir) {
        if (dir.endsWith(File.separator))
            return buildDir(new File(dir));
        String path = dir.substring(0, dir.lastIndexOf(File.separator));

        return buildDir(new File(path));
    }

    /**
     * 
     *
     * @param dirs - 
     * @return 
     */
    public static boolean buildDir(File dirs) {
        return dirs.exists() || dirs.mkdirs();
    }

    /**
     * 
     *
     * @param dir - 
     * @param file - 
     * @return ?
     */
    public static String join(String dir, String file) {
        return (dir.endsWith(File.separator) ? dir : dir + File.separator)
                + (file.startsWith(File.separator) ? file.substring(1) : file);
    }

    /**
     * 
     *
     * @param source - ?
     * @param destination - ?
     * @return ??
     * @throws IOException
     */
    public static boolean transferFile(File source, File destination) throws IOException {
        if (!destination.exists()) {
            logger.debug(destination.getPath());
            destination.createNewFile();
        }

        FileChannel src, dest;

        src = new FileInputStream(source).getChannel();
        dest = new FileOutputStream(destination).getChannel();

        long count = 0;
        long size = src.size();

        while ((count += dest.transferFrom(src, count, size - count)) < size)
            ;

        return true;
    }

    /**
     * ?
     *
     * @param bytes - ?
     * @param destination - 
     * @return 
     * @throws IOException
     */
    public static boolean transferFile(byte[] bytes, File destination) throws IOException {
        FileOutputStream fos = new FileOutputStream(destination);
        fos.write(bytes);
        fos.close();
        return true;
    }
}