zoukankan      html  css  js  c++  java
  • FileUtils

    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.OutputStream;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.log4j.Logger;
    
    public class FileUtils {
    
        /**
         * 读取一个文件夹下所有文件的绝对路径。忽略此文件夹下的问价夹
         * 
         * @param absFolderPath
         *            文件夹绝对路径
         * @return List<String>
         */
        public static List<String> readFilePath(String absFolderPath) {
            List<String> paths = new ArrayList<String>();
            File file = new File(absFolderPath);
            // 文件夹
            if (file.isDirectory()) {
                for (File childFile : file.listFiles()) {
                    // 不是文件夹并且没有被操作
                    if (!childFile.isDirectory()) {
                        // 路径通过"/"手动创建,避免因window下出现反斜杠 路径的情况
                        paths.add(absFolderPath + "/" + childFile.getName());
                    }
                }
            }
            return paths;
        }
    
        /**
         * 通过文件的绝对路径删除文件,如果传入是文件夹路径,忽略并返回false
         * 
         * @param absFilePath
         *            文件绝对路径
         * @return boolean
         */
        public static boolean deleteFile(String absFilePath) {
            boolean result = false;
            File file = new File(absFilePath);
            if (!file.isDirectory()) {
                file.delete();
                result = true;
            }
            return result;
        }
    
        /**
         * 删除问价夹下所有文件
         * 
         * @param absFolderPath
         * @return boolean
         */
        public static boolean deleteAllFileInTheFolder(String absFolderPath) {
            boolean result = true;
            for (String filePath : FileDealUtil.readFilePath(absFolderPath)) {
                result = result && FileDealUtil.deleteFile(filePath);
            }
            return result;
        }
    
        /**
         * 将文件移动到指定问价夹下
         * 
         * @param filePath
         *            文件路径
         * @param folderPath
         *            文件夹路径
         * @return String 移动后文件路径,filePath为文件夹或folderPath为文件或文件正在被其他进程打开无法移动返回null
    
         */
        public static String moveFile(String filePath, String folderPath) {
            String path = null;
    
            File file = new File(filePath);
            if (file.isDirectory())
                return path;
            File folder = new File(folderPath);
            if (!folder.exists()) {
                folder.mkdirs();
            }
            if (!folder.isDirectory())
                return path;
    
            File nowFile = new File(folderPath + "/" + file.getName());
            if (nowFile.exists())
                nowFile.delete();
            if (file.renameTo(new File(folder, file.getName())))
                path = folderPath + "/" + file.getName();
            return path;
        }
    
        /**
         * 得到文件输入流
         * 
         * @param path
         *            绝对路径
         * @return InputStream
    
         */
        public static InputStream getFile(String path) {
            // First try to read from the file system ...
            File file = new File(path);
            if (file.exists() && file.canRead()) {
                try {
                    return new FileInputStream(file);
                } catch (FileNotFoundException e) {
                    // continue
                    return null;
                }
            }
            return null;
        }
    
        /**
         * 判断文件夹是否存在 void
         * 
         */
        public static boolean existFolder(String folderPath) {
            File file = new File(folderPath);
            return file.exists();
        }
    
        /**
         * 创建文件夹
         * 
         * @param folderPath
         *            void
         */
        public static void creatFolder(String folderPath) {
            if (!existFolder(folderPath)) {
                File file = new File(folderPath);
                file.mkdirs();
            }
        }
    
        /**
         * 获取文件大小 如果文件存在并且不是目录,返回文件大小,如果文件不存在或者是目录,返回0
         * 
         * @return Long 单位bytes
         */
        public static Long getFileSize(String filePath) {
            File file = new File(filePath);
            if (file.exists() && !file.isDirectory()) {
                return file.length();
            } else {
                return 0L;
            }
        }
    
        /**
         * 从文件路径中分离出文件名
         * 
         * @param filePath
         * @return String
         */
        public static String splitFileName(String filePath) {
            return filePath.substring(filePath.lastIndexOf("/") + 1);
        }
    
        /**
         * 
         * @param filePath
         * @param inputStream
         * @return boolean
         */
        public static boolean writeFile(String filePath, InputStream inputStream) {
            OutputStream outputStream = null;
            try {
                outputStream = new FileOutputStream(filePath);
    
                int bytesRead = 0;
                byte[] buffer = new byte[2048];
                while ((bytesRead = inputStream.read(buffer, 0, 2048)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
                return true;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            } finally {
                try {
                    outputStream.close();
                    inputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
  • 相关阅读:
    最长公共前缀
    无重复字符的最长子串
    文章采集代码
    网络验证常见的攻击方式与防御手段
    初创公司如何避免服务器被攻击
    拒绝ssh远程暴力破解
    我公司开了7年,靠的就是这套顶级销售打法撑下来!
    顶级销售的十个习惯,轻松签下百万千万合同!(值得背下来)
    顶级销售高手总结的 9 个方面
    一位顶级销售高手总结的“销售心得”!
  • 原文地址:https://www.cnblogs.com/lyunyu/p/4996979.html
Copyright © 2011-2022 走看看