zoukankan      html  css  js  c++  java
  • 经常使用的文件工具类

    类说明:

    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;
        }
  • 相关阅读:
    20191024-6 Alpha发布用户使用报告
    【第三章】MySQL数据库的字段约束:数据完整性、主键、外键、非空、默认值、自增、唯一性
    【第六章】MySQL日志文件管理
    【第四章】MySQL数据库的基本操作:数据库、表的创建插入查看
    【第一章】MySQL数据概述
    【Linux运维】LNMP环境配置
    【Linux 运维】linux系统修改主机名
    【Linux 运维】linux系统查看版本信息
    【Linux 运维】Centos7初始化网络配置
    50、树中两个节点的公共祖先
  • 原文地址:https://www.cnblogs.com/yuwenfeng/p/3075802.html
Copyright © 2011-2022 走看看