文件File工具类

来源:互联网 发布:更改歌曲属性数据无效 编辑:程序博客网 时间:2024/05/22 03:12

FileUtil.java

package com.lmb.common.util;/** * 文件操作处理模块: *          用于文件对象处理 */import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.Closeable;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;import com.lmb.exception.CustomizeException;public class FileUtil {    private static final Log log = LogFactory.getLog(FileUtil.class);    private static final String DEFAULTENCODING = "UTF-8";    public FileUtil() {        super();    }    /**     * 关闭文件流对象     *      * @param is     */    public static void closeable(Closeable is) {        if (is != null) {            try {                is.close();            } catch (IOException e) {                log.error(e);            }        }    }    /**     * 关闭XMLWriter对象     *      * @param writer     */    public static void closeXMLWriter(XMLWriter writer) {        if (writer != null) {            try {                writer.close();            } catch (IOException e) {                log.error(e);            }        }    }    /**     * 根据消息生成Document文档     *      * @param msg     * @return     * @throws CustomizeException     */    public static Document buildDocument(String msg) throws CustomizeException {        InputStream is = null;        Document doc = null;        try {            is = new ByteArrayInputStream(msg.getBytes(DEFAULTENCODING));            SAXReader reader = new SAXReader();            doc = reader.read(is);        } catch (UnsupportedEncodingException e) {            throw new CustomizeException(e);        } catch (DocumentException e) {            throw new CustomizeException(e);        } finally {            closeable(is);        }        return doc;    }    /**     * 保存XML文件信息     *      * @param filename     * @param msg     * @throws CustomizeException     */    public static void saveXmlFile(String filename, String msg) throws CustomizeException {        XMLWriter writer = null;        try {            File f = new File(filename.substring(0, filename.lastIndexOf(File.separatorChar)));            f.mkdirs();            OutputFormat format = OutputFormat.createPrettyPrint();            format.setEncoding(DEFAULTENCODING);            writer = new XMLWriter(new FileOutputStream(new File(filename)), format);            writer.write(buildDocument(msg));        } catch (IOException e) {            throw new CustomizeException(e);        } finally {            closeXMLWriter(writer);        }    }    /**     * 读取XML文件内容     *      * @param filename     * @return     * @throws CustomizeException     */    public String readXMLFile(String filename) throws CustomizeException {        Document document = null;        try {            SAXReader reader = new SAXReader();            document = reader.read(new File(filename));        } catch (DocumentException e) {            throw new CustomizeException(e);        }        return document.asXML();    }    /**     * 保存txt文件     * @param file     * @param content     * @return     */    public static void saveTxtFile(String filename, String content) throws CustomizeException{        PrintWriter print = null;        FileWriter fw = null;        try {            File f = new File(filename.substring(0, filename.lastIndexOf(File.separatorChar)));            f.mkdirs();            fw = new FileWriter(filename);             print = new PrintWriter(fw);             print.write(content);        }catch (IOException e) {            e.printStackTrace();        }finally{            try {                fw.close();            } catch (IOException e) {            }            print.close();        }    }    /**     * 读取txt文件信息     *      * @param filename     * @return     * @throws CustomizeException     */    public static String readTxtFile(String filename) throws CustomizeException{        BufferedReader reader = null;        StringBuffer content = new StringBuffer();        try{            reader = new BufferedReader(new FileReader(filename));            String line;            while((line = reader.readLine()) != null){                content.append(line);            }        }catch(IOException e){            throw new CustomizeException(e);        }finally{            closeable(reader);        }        return content.toString();    }    /**     * 读取文件内容     * @param filename     * @return     * @throws CustomizeException     */    public static byte[] readByteFile(String filename) throws CustomizeException{        byte[] result = null;         BufferedInputStream is = null;        ByteArrayOutputStream bos = null;        try{             is = new BufferedInputStream(new FileInputStream(filename));             bos = new ByteArrayOutputStream();              //读取源文件             result = new byte[is.available()];                 is.read(result, 0, result.length);         }catch(IOException e){            throw new CustomizeException(e);        }finally{            FileUtil.closeable(bos);            FileUtil.closeable(is);        }        return result;    }    /**     * 保存文件     * @param bytes     * @param fileName     */    public static void saveByteFile(byte[] bytes, String fileName) throws CustomizeException{        FileOutputStream fos=null;           try{               fos = new FileOutputStream(fileName,true);             fos.write(bytes);               fos.flush();             }catch(Exception e){               throw new CustomizeException(e);             }finally{               FileUtil.closeable(fos);        }       }}

更多有关文件操作I/O流的文章请看这里:

I/O流(一)

I/O流(二)

I/O流(三)—对象的序列化和反序列化

I/O流(四)—java如何添加到文件尾

1 0
原创粉丝点击