zoukankan      html  css  js  c++  java
  • 文件操作Utils方法1

    import com.pinnet.common.utils.CommonUtils;
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.nio.charset.Charset;
    import java.nio.file.Files;
    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.List;
    import java.util.Locale;
    import java.util.Properties;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import org.apache.commons.io.LineIterator;
    import org.apache.commons.lang.StringUtils;
    import org.apache.tools.zip.ZipEntry;
    import org.apache.tools.zip.ZipFile;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    import static java.nio.file.StandardOpenOption.CREATE;
    import static java.nio.file.StandardOpenOption.WRITE;


    public class FileCommonUtils {

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

    public static boolean isExist(String path) {
    File file = new File(path);

    return (file.exists());
    }

    public static boolean isFile(String path) {
    if (CommonUtils.validateFile(path)) {
    File file = new File(path);
    return isFile(file);
    }
    return false;
    }

    public static boolean isDirectory(String path) {
    if (!CommonUtils.validateFile(path)) {
    return false;
    }
    File file = new File(path);
    return isDirectory(file);
    }

    public static boolean isFile(File file) {
    return (file.getName().contains("."));
    }

    public static boolean isDirectory(File file) {
    return (!(file.getName().contains(".")));
    }

    public static boolean create(String path) {
    if (isFile(path)) {
    return createFile(path);
    }
    return createFolder(path);
    }

    public static boolean createFolder(String path) {
    File file = new File(path);
    if (isFile(path)) {
    return false;
    }
    if (!(file.exists())) {
    return file.mkdirs();
    }
    return false;
    }

    public static String getAbsolutePath(String file) {
    File f = new File(file);
    return f.getAbsolutePath();
    }

    public static boolean createFile(String path) {
    if (!CommonUtils.validateFile(path)) {
    logger.error("createFile file path is invalid , path = {}.", path);
    return false;
    }

    File file = new File(path);
    if (isDirectory(path)) {
    return false;
    }
    if (!(file.exists())) {
    String folder = file.getParent();
    createFolder(folder);
    try {
    return file.createNewFile();
    } catch (IOException e) {
    // e.printStackTrace();
    // C_381613 FORTIFY.System_Information_Leak
    logger.error("createFile error :", e);
    }
    }
    return false;
    }

    public static boolean rename(String src, String des) {
    if ((isFile(src)) && (isFile(des)))
    return renameFile(src, des);
    if ((isDirectory(src)) && (isDirectory(des))) {
    return renameFolder(src, des);
    }
    return false;
    }

    public static boolean renameFolder(String src, String des) {
    return renameFile(src, des);
    }

    public static boolean renameFile(String src, String des) {
    File srcFile = new File(src);
    File desFile = new File(des);
    if ((!(srcFile.exists())) || (desFile.exists())
    || (!(srcFile.getName().equals(srcFile.getName())))) {
    return false;
    }
    return srcFile.renameTo(desFile);
    }

    public static boolean move(String src, String des) {
    File desFile = new File(des);
    if ((isFile(src)) && (isFile(des))) {
    if (desFile.exists()) {
    if (!desFile.delete()) {
    logger.warn("delete file error, file is {}", desFile.getAbsolutePath());
    }
    }
    return moveFile(src, des);
    }
    if ((isDirectory(src)) && (isDirectory(des))) {
    if (desFile.exists()) {
    if (!desFile.delete()) {
    logger.warn("delete file error, file is {}", desFile.getAbsolutePath());
    }
    }
    return moveFolder(src, des);
    }
    return false;
    }

    public static boolean moveFolder(String src, String des) {
    boolean result = copyFolder(src, des);
    delete(src);
    return result;
    }

    public static boolean moveFile(String src, String des) {
    boolean result = copyFile(src, des);
    delete(src);
    return result;
    }

    public static boolean copy(String src, String des) {
    File desFile = new File(des);
    if ((isFile(src)) && (isFile(des))) {
    if (desFile.exists()) {
    if (!desFile.delete()) {
    logger.warn("desFile delete file failed , file is {}" , desFile.getAbsolutePath());
    }
    }
    return copyFile(src, des);
    }
    if ((isDirectory(src)) && (isDirectory(des))) {
    if (desFile.exists()) {
    if (!desFile.delete()) {
    logger.warn("desFile delete file failed , file is {}" , desFile.getAbsolutePath());
    }
    }
    return copyFolder(src, des);
    }
    return false;
    }

    public static boolean copyFile(String src, String des) {
    create(des);
    if (src.equals(des)) {
    return false;
    }
    InputStream inStream = null;
    FileOutputStream fs = null;
    int byteread;
    try {
    byteread = 0;
    File oldfile = new File(src);
    if (oldfile.exists()) {
    inStream = new FileInputStream(src);
    fs = new FileOutputStream(des);
    byte[] buffer = new byte[1024];
    while ((byteread = inStream.read(buffer)) != -1) {
    fs.write(buffer, 0, byteread);
    }
    inStream.close();
    inStream = null;
    fs.close();
    fs = null;
    }
    } catch (Exception e) {
    logger.error("copyFile error,", e);
    byteread = 0;
    return false;
    } finally {
    try {
    if (inStream != null)
    inStream.close();
    } catch (Exception e) {
    logger.error("copyFile close inStream error,", e);
    }
    try {
    if (fs != null)
    fs.close();
    } catch (Exception e) {
    logger.error("copyFile close fs error,", e);
    }
    }
    return true;
    }

    public static boolean copyFolder(String src, String des) {
    create(des);
    File srcFile = new File(src);
    File desFile = new File(des);
    if (!(desFile.exists())) {
    createFolder(des);
    }
    String[] srcs = srcFile.list();
    if (srcs != null) {
    for (String f : srcs) {
    File ff = new File(srcFile.getAbsoluteFile() + "/" + f);
    if (ff.isFile())
    copyFile(ff.getAbsolutePath(), des + "/" + ff.getName());
    else {
    copyFolder(ff.getAbsolutePath(), des + "/" + ff.getName());
    }
    }
    }
    return true;
    }

    public static boolean delete(String path) {
    if (isFile(path))
    deleteFile(path);
    else {
    deleteFolder(path);
    }
    return true;
    }

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

    public static boolean deleteFolder(String path) {
    File file = new File(path);
    if (isFile(path)) {
    return true;
    }
    String[] files = file.list();
    if (files != null) {
    for (String f : files) {
    File ff = new File(file.getAbsoluteFile() + "/" + f);
    if (ff != null) {
    if (ff.isDirectory())
    deleteFolder(ff.getAbsolutePath());
    else {
    deleteFile(ff.getAbsolutePath());
    }
    }
    }
    }

    if (!file.delete()) {
    logger.warn("delete file error, file is {}", file.getAbsolutePath());
    }
    return true;
    }
  • 相关阅读:
    hibernate之初学复合主键
    hibernate之初学增删改查
    hibernate之配置文件帮助文档
    hibernate之初学项目搭建
    Python调用OpenCV读显写
    C++写入txt
    C++逐词读取txt
    C++逐行读取txt
    C#调用C++系列二:传结构体
    C#调用C++系列一:简单传值
  • 原文地址:https://www.cnblogs.com/sg9527/p/8568928.html
Copyright © 2011-2022 走看看