⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileutil.java

📁 一个关于tlms的一个小程序 看看能否帮助到别人
💻 JAVA
字号:
package com.szmx.framework.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;


/**
 * User     : bing.zhang@szmx.com
 * Date     : 2005-10-12 10:42:37
 * Version  : 0.1
 * updateBy :
 * updateDt :
 */
public class FileUtil
{
    private static Log logger = LogFactory.getLog(FileUtil.class);

    /**
     * Write a serializable object into a local file
     * @param obj
     * @param destFileFullPathName
     * @return boolean
     * author: BZhang
     */
    public static boolean writeSerializableObject2File(Object obj, String destFileFullPathName) {
        ObjectOutputStream out = null;
        try {
            File f = createFile(destFileFullPathName);
            out = new ObjectOutputStream(new FileOutputStream( f ));
            out.writeObject( obj );
            return true;
        } catch (Exception ex) {
            logger.error(ex);
            return false;
        } finally{
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                logger.error(e);
            }
        }
    }

    /**
     * read file to the serialized object
     * @param srcFileFullPathName
     * @return Object
     * author: BZhang
     */
    public static Object readSerializableObjectFromFile(String srcFileFullPathName) {
        ObjectInputStream in = null;
        try {
            in = new ObjectInputStream(new FileInputStream( srcFileFullPathName ));
            return in.readObject();
        } catch (Exception ex) {
            logger.error(ex);
            return null;
        } finally{
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                logger.error(e);
            }
        }
    }

    /**
     *
     * @param srcFileFullPathName
     * @param zipedFileFullPathName
     * @return boolean
     * author: BZhang
     */
    public static boolean zipFile(String srcFileFullPathName, String zipedFileFullPathName) {
        ZipOutputStream zipOut = null;
        FileInputStream fin = null;

        try {
            zipOut = new ZipOutputStream(new FileOutputStream( zipedFileFullPathName ));
            fin = new FileInputStream( srcFileFullPathName );

            String fileName = srcFileFullPathName.substring(srcFileFullPathName.lastIndexOf("\\")+1, srcFileFullPathName.length());
            zipOut.putNextEntry( new ZipEntry(fileName) );

            int byteRead;
            byte[] buff = new byte[1024];
            while ((byteRead = fin.read(buff)) != -1) {
                zipOut.write(buff, 0, byteRead);
            }

            return true;
        } catch (Exception ex) {
            logger.error(ex);
            return false;
        } finally{
            try {
                if (fin != null) {
                    fin.close();
                }
                if (zipOut != null) {
                    zipOut.close();
                }
            } catch (IOException e) {
                logger.error(e);
            }
        }
    }

    /**
     *
     * @param zipedFileFullPathName
     * @param upzipFileFullPathName
     * @return boolean
     * author: BZhang
     */
    public static boolean upzipFile(String zipedFileFullPathName, String upzipFileFullPathName) {
        ZipInputStream zipIn = null;
        FileOutputStream fout = null;

        try {
            zipIn = new ZipInputStream( new FileInputStream(zipedFileFullPathName) );
            File f = new File( upzipFileFullPathName );
                 f.createNewFile();
            fout = new FileOutputStream(f);

            ZipEntry z = zipIn.getNextEntry();
            if (z != null) {
                int byteRead;
                byte[] buff = new byte[1024];
                while ((byteRead = zipIn.read(buff)) != -1) {
                    fout.write(buff, 0, byteRead);
                }
            }

            return true;
        } catch (Exception ex) {
            logger.error(ex);
            return false;
        } finally{
            try {
                if (fout != null) {
                    fout.close();
                }
                if (zipIn != null) {
                    zipIn.close();
                }
            } catch (IOException e) {
                logger.error(e);
            }
        }
    }

    public static void writeStream2File(InputStream inStream, String fileFullPathname) {
        try {
            File file = new File(fileFullPathname);
            if (! file.exists()) {
                file.createNewFile();
            }

            FileOutputStream foutStream = new FileOutputStream(file);
            int byteRead;
            byte[] buff = new byte[1024];
            while ((byteRead = inStream.read(buff)) != -1) {
                foutStream.write(buff, 0, byteRead);
            }
            inStream.close();
            foutStream.close();
        } catch(Exception ex) {
            logger.error(ex);
        }
    }

    public static void writeFile2Stream(String fileFullPathName, OutputStream outStream) {
        try {
            FileInputStream finStream = new FileInputStream(fileFullPathName);
            int byteRead;
            byte[] buff = new byte[1024];
            while ((byteRead = finStream.read(buff)) != -1) {
                outStream.write(buff, 0, byteRead);
            }
            finStream.close();
            outStream.close();
        } catch(Exception e) {
            logger.error(e);
        }
    }

    public static void deleteFile(String fileFullPathName) {
        File f = new File( fileFullPathName );
        if (f.exists()) {
            f.delete();
        }
    }

    public static void deleteFiles(String folderFullPathName) {
        File f = new File( folderFullPathName );
        if (f.isDirectory()) {
            File [] fs = f.listFiles();
            for (int i=0; i<fs.length; i++) {
                fs[i].delete();
            }
        }
    }

    public static File createFile(String fileFullPathname) {
        File folder = new File(fileFullPathname.substring(0, fileFullPathname.lastIndexOf("\\")));
        File file = new File(fileFullPathname);
        try {
            if(! folder.exists()) {
                folder.mkdirs();
            }

            if (! file.exists()) {
                file.createNewFile();
            }
        } catch(Exception ex) {
            logger.error(ex);
        }
        return file;
    }

    public static boolean existsFile(String fileFullPathname) {
        File file = new File(fileFullPathname);
        return file.exists();
    }

    /**
     *
     * @param srcFile
     * @param renameFile
     * @return boolean true=success false=otherwise
     */
    public static boolean renameFile(File srcFile,File renameFile){
        return srcFile.renameTo(renameFile);
    }

    /**
     * the same file path, only rename the file
     * @param srcFile
     * @param renameFileName
     * @return boolean true=success false=otherwise
     */
    public static boolean renameFile(File srcFile,String renameFileName){
        String path = srcFile.getPath();
        String renameFilePath = path.substring(0,path.lastIndexOf("\\")) + "\\"+ renameFileName;
        return srcFile.renameTo(new File(renameFilePath));
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -