经常使用的文件工具类

类说明:

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;
    }

 

posted on 2013-05-13 15:14  ywf—java  阅读(229)  评论(0编辑  收藏  举报

导航