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;
        }
  • 相关阅读:
    2014 ACM/ICPC Asia Regional Guangzhou Online HDU 5024 Wang Xifeng's Little Plot
    HDU 5015 2014 ACM/ICPC Asia Regional Xi'an Online 233 Matrix
    POJ 1269 Intersecting Lines(直线相交的判断)
    POJ 3304 Segments
    POJ 2318 TOYS
    The 2014 ACM-ICPC Asia Mudanjiang Regional First Round C ZOj 3811
    unique函数(先记下来)
    计算几何常用算法(数学知识)
    HUD 3461 codelock 纯题意解释,不含思想
    POJ 1182 食物链
  • 原文地址:https://www.cnblogs.com/yuwenfeng/p/3075802.html
Copyright © 2011-2022 走看看