• 经常使用的文件工具类


    类说明:

    getFilePrefix()取文件名称
    getFileSufix()取文件后缀名
    copyFile()复制文件

    View Code
    package com.converter.utils;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    
    /**
     * 文件类
     * @author ywf
     *
     */
    public class FileUtils {
        /**
         * 取文件名称
         * @param fileName
         * @return
         */
        public static String getFilePrefix(String fileName){
            int splitIndex = fileName.lastIndexOf(".");
            return fileName.substring(0, splitIndex);
        }
        /**
         * 取文件后缀名
         * @param fileName
         * @return
         */
        public static String getFileSufix(String fileName){
            int splitIndex = fileName.lastIndexOf(".");
            return fileName.substring(splitIndex + 1);
        }
        /**
         *拷贝文件
         * @param inputFile
         * @param outputFile
         * @throws FileNotFoundException
         */
        public static void copyFile(String inputFile,String outputFile) throws FileNotFoundException{
            File sFile = new File(inputFile);
            File tFile = new File(outputFile);
            FileInputStream fis = new FileInputStream(sFile);
            FileOutputStream fos = new FileOutputStream(tFile);
            int temp = 0;  
            try {  
                while ((temp = fis.read()) != -1) {//判断内容不为空的一种简单写法  
                    fos.write(temp);  
                }
            } catch (IOException e) {  
                e.printStackTrace();  
            } finally{
                try {
                    fis.close();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } 
        }
    }
    /**
         * 获取指定目录下的文件结构
         * 
         * @param filePath
         *            指定的目录
         * @param filter
         * @return 该指定目录下的所有的文件列表(只包含文件名),格式为HashMap,key为文件夹路径, value值为文件夹下的文件列表
         */
        @SuppressWarnings({ "unchecked", "rawtypes" })
        public static HashMap<String, ArrayList> getPosterity(String filePath,
                FileFilter filter) {
            File root = new File(filePath);
            ArrayList/* <File> */dirs = new ArrayList/* <File> */();
            HashMap<String, ArrayList> filesList = new HashMap<String, ArrayList>();
            dirs.add(root);
            int index = 0;
            while (index < dirs.size()) {
                File cur = (File) dirs.get(index);
                File[] children = cur.listFiles();
                ArrayList files = new ArrayList();
                for (int i = 0; i < children.length; i++) {
                    File f = children[i];
                    if (filter == null || filter.accept(f)) {
                        if (f.isDirectory()) {
                            dirs.add(f);
    
                        } else {
                            files.add(f.getName());
                        }
                    }
                }
                String filepath = cur.getPath();
                filepath = filepath.replaceAll("\\\\", "/");
                if (files.size() > 0) {
                    filesList.put(filepath, files);
                }
                index++;
            }
            return filesList;
        }
  • 相关阅读:
    解决org.apache.jasper.JasperException: Failed to load or instantiate TagLibraryVal
    学java快2月了,对其应该有点清晰的认识了
    Linux(CentOS)挂载移动硬盘
    SEVERE: A child container failed during start
    JSTL 标签 详解
    转载自MSDN:Implementing a Membership Provider
    一个比较实用的服务器端模拟客户端Alert的代码
    简单的SQL分页法
    转载:Global.asax 文件 使用参考
    转载:缓存 Cache
  • 原文地址:https://www.cnblogs.com/yuwenfeng/p/3075802.html
Copyright © 2020-2023  润新知