文件操作类

package com.yunmall.framework.core.util;

import info.monitorenter.cpdetector.CharsetPrinter;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.MalformedURLException;

import com.yunmall.framework.core.log.LogTrace;

/**
 * 文件操作类
 * 
 * @version 1.0
 
 * @createDate 2012-9-14
 
 * @modifyDate 2012-9-14
 */
public class FileUtil {
    private static final LogTrace log = new LogTrace("FileUtil");
    /**
     * 获取文件的后缀名
     * 
     * @param fileName
     *            文件名
     * @return 后缀名
     */
    public static String getExtentsName(String fileName) {
        if (fileName.contains(".")) {
            int pos = fileName.lastIndexOf(".");
            return fileName.substring(pos);
        }
        return "";
    }

    /**
     * 以指定编码读取文件内容
     * 
     * @param file
     *            文件
     * @param encode
     *            编码
     * @return 文件内容
     */

    public static String readFile(File file, String encode) {
        StringBuffer content = new StringBuffer("");
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(new FileInputStream(file), encode));
            String str = "";
            while ((str = in.readLine()) != null) {
                content.append(str + "\n");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }
        return content.toString();
    }

    /**
     * 以指定编码读取文件内容
     * 
     * @param pathname
     *            文件路径
     * @param encode
     *            编码
     * @return 文件内容
     */
    public static String readFile(String pathname, String encode) {
        File file = new File(pathname);
        return readFile(file, encode);
    }

    /**
     * 读取文件内容,编码默认为utf-8
     * 
     * @param pathname
     *            文件路径
     * @return 文件内容
     */
    public static String readFile(String pathname) {
        return readFile(pathname, "UTF-8");
    }

    /**
     * 读取文件内容,编码默认为utf-8
     * 
     * @param file
     *            文件
     * @return 文件内容
     */
    public static String readFile(File file) {
        return readFile(file, "UTF-8");
    }

    /**
     * 获取文件的编码类型
     * 
     * @param file
     *            文件
     * @return 文件的编码类型
     */
    public static String getEncode(File file) {
        String encode = "";

        CharsetPrinter charsetPrinter = new CharsetPrinter();
        try {
            encode = charsetPrinter.guessEncoding(file);
        } catch (MalformedURLException e) {
            log.error(e.getMessage());
        } catch (IOException e) {
            log.error(e.getMessage());
        }

        return encode;
    }

    /**
     * 按指定编码写入文件
     * 
     * @param file
     *            文件
     * @param encoding
     *            指定编码
     * 
     * @param content
     *            文件内容
     */
    public static void write(File file, String encoding, String content) {
        if (file == null || !file.exists() || file.isDirectory()) {
            return;
        }
        Writer out = null;
        try {
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file), encoding);
            out = new BufferedWriter(osw);
            out.write(content);
        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }
    }

    /**
     * 转换文件编码类型为UTF-8
     * 
     * @param file
     *            文件
     * @return file
     */
    public static File converterUtf8Encode(File file) {
        String encode = getEncode(file);
        encode = StringUtil.isEmpty(encode) ? "UTF-8" : encode;
        String content = "";

        if (encode.equals("UTF-8")) {
            return file;
        }

        content = readFile(file, "GBK");

        write(file, "UTF-8", content.toString());

        return file;
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值