All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hecloud.runtime.common.file.FileOperation Maven / Gradle / Ivy

package com.hecloud.runtime.common.file;

import com.hecloud.runtime.common.model.GenericResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;

/**
 * @author LoveinBJ
 */
public class FileOperation {

    private static Logger logger = LoggerFactory.getLogger(FileOperation.class);

    public static boolean deleteFile(String path) {
        File file = new File(path);
        if (file.isFile() && file.exists()) {
            return file.delete();
        }
        return false;
    }

    public static boolean deleteDirectory(String path) {
        String[] allPath = path.split(FileSeparator.SEPARATOR);
        String dirPath = "";
        for (int i = 0; i < allPath.length - 1; i++) {
            dirPath += FileSeparator.SEPARATOR + allPath[i];
        }
        if (!dirPath.endsWith(FileSeparator.SEPARATOR)) {
            dirPath += FileSeparator.SEPARATOR;
        }
        File dirFile = new File(dirPath);
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return false;
        }
        if (dirFile.delete()) {
            return true;
        }
        return false;
    }

    public static boolean moveFile(String source, String target) {
        File file = new File(source);
        if (!file.exists()) {
            return false;
        }
        String[] p = file.getAbsolutePath().split(FileSeparator.SEPARATOR);
        String filename = p[p.length - 1];
        File afile = new File(target);
        if (!afile.exists()) {
            afile.mkdirs();
        }
        String newPath = afile.getAbsolutePath() + File.separatorChar + filename;
        if (file.renameTo(new File(newPath))) {
            return true;
        }
        return false;
    }

    public static boolean makeDirs(String folderName) {
        if (folderName == null || folderName.isEmpty()) {
            return false;
        }
        File folder = new File(folderName);
        return (folder.exists() && folder.isDirectory()) ? true : folder.mkdirs();
    }

    public static byte[] read(File file) throws Exception {
        if (file.exists()) {
            try (FileInputStream inputStream = new FileInputStream(file);) {
                Long fileSize = file.length();
                byte[] buffer = new byte[fileSize.intValue()];
                int count = 0;
                while ((count = inputStream.read(buffer)) > 0) {

                }
                return buffer;
            } catch (Exception e) {
                logger.error("Read File [{}] Exception!", file.getName(), e);
                return null;
            }
        } else {
            return null;
        }
    }

    public static GenericResult read(String fileName) throws Exception {
        File file = new File(fileName);
        byte[] fileContent = read(file);
        if (null != fileContent && fileContent.length > 0) {
            return new GenericResult<>(true, "success", fileContent);
        } else {
            return new GenericResult<>(false, "文件内容为空!");
        }

    }

    public static String readToString(String fileName) throws Exception {
        String encoding = "UTF-8";
        GenericResult readResult = read(fileName);
        try {
            if (readResult.isSuccess()) {
                return new String(readResult.getData(), encoding);
            } else {
                return null;
            }
        } catch (UnsupportedEncodingException e) {
            logger.error("UnsupportedEncodingException!", e);
            return null;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy