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

📄 fileutil.java

📁 数据刷新程序,用于不同数据库之间德数据传递
💻 JAVA
字号:
package com.main.apps.common;

import java.io.*;

 
public class FileUtil
{
    
    public synchronized static void printEvent(String msg, String fileName)
    {
        msg = new String("时间:" + TimeUtil.getTime(TimeUtil.YYMMDDhhmmss) + " 事件消息:  "
                + msg);
        if (fileName != null)
            printToFile(msg, fileName);
        else
            print(msg);
        return;
    }

    public synchronized static void debug(String message, String fileName)
    {
        if (fileName == null)
        {
            print(message);
        }
        printToFile("时间:" + TimeUtil.getTime(TimeUtil.YYMMDDhhmmssxxx) + " 信息:"
                + message, fileName);
    }

    /**
     * 记录应程序本身发生的错误,主要给程序员观察。
     * 
     * @param e
     *            一个Exception
     * @param mobile
     *            用户手机号码
     * @param msg
     *            用户发送的消息
     * @param fileName
     *            日志文件的路径及名称
     */
    public synchronized static void printError(Throwable e, String msg,
            String fileName)
    {
        StringBuffer errors = new StringBuffer(100);
        errors.append("时间:");
        errors.append(TimeUtil.getTime(TimeUtil.YYMMDDhhmmssxxx));
        errors.append(" 消息:");
        errors.append(msg);
        errors.append(" Exception: ");
        if (fileName != null)
        {
            printToFile(errors.toString().trim(), fileName);
            try
            {
                e.printStackTrace(new PrintWriter(
                        new FileWriter(fileName, true), true));//
            }
            catch (Exception ex)
            {
            }
        }
        else
            print(errors.toString().trim());
        return;
    }

    public synchronized static void printState(String msg, String fileName)
    {
        if (fileName != null)
        {
            printToFile(msg, fileName);
        }
        else
            print(msg);
        return;
    }

    /**
     * 写文件
     * 
     * @param msg
     * @param fileName
     */
    public synchronized static void writeFile(String msg, String fileName)
    {
        if (fileName != null)
            printToFile(msg, fileName, true);
        else
            print(msg);
        return;
    }

    /**
     * 把错误消息打印到屏幕上
     * 
     * @param msg
     *            错误消息
     */
    private static void print(String msg)
    {
        System.out.println(msg);
    }

    /**
     * 把消息打印到指定文件
     * 
     * @param msg
     *            错误消息
     * @param fileName
     *            指定的文件
     */

    private static void printToFile(String msg, String fileName) // 打印到文件中
    {
        BufferedWriter mBufWriter = null;
        try
        {
            if (!createFile(fileName))
                return;
            FileWriter fileWriter = new FileWriter(fileName, true);
            mBufWriter = new BufferedWriter(fileWriter);

            mBufWriter.write(msg);
            mBufWriter.newLine();

            mBufWriter.flush();
            mBufWriter.close();
        }
        catch (Throwable e)
        {
            try
            {
                mBufWriter.close();
            }
            catch (Throwable t)
            {
            }
            ;
        }
        return;
    }

    /**
     * 用来创建文件和文件夹
     * 
     * @param fileName
     *            文件或文件夹名称
     * @return
     * @throws IOException
     * @throws Exception
     */

    private static boolean createFile(String fileName) throws IOException,
            Exception
    {
        File file = new File(fileName);
        if (file.exists()) /* does file exist? If so, can it be written to */
        {
            if (file.canWrite() == false)
                return false;
        }
        else
        {
            String path = null; /* Does not exist. Create the directories */

            int firstSlash = fileName.indexOf(File.separatorChar);
            int finalSlash = fileName.lastIndexOf(File.separatorChar);

            if (finalSlash == 0)
            { /* error, not valid path */
            }
            else if (finalSlash == 1) /* UNIX root dir */
            {
                path = File.separator;
            }
            else if (firstSlash == finalSlash)
            { /* for example c:\ Then make sure slash is part of path */
                path = fileName.substring(0, finalSlash + 1);
            }
            else
            {
                path = fileName.substring(0, finalSlash);
            }

            File dir = new File(path);
            dir.mkdirs();
        }
        return true;
    }

    /**
     * 输出到文件
     * 
     * @param msg
     * @param fileName
     * @param flag
     */
    private static void printToFile(String msg, String fileName, boolean flag)
    {
        BufferedWriter mBufWriter = null;
        try
        {
            if (!createFile(fileName))
                return;
            FileWriter fileWriter = new FileWriter(fileName, flag);
            mBufWriter = new BufferedWriter(fileWriter);

            mBufWriter.write(msg);
            mBufWriter.newLine();

            mBufWriter.flush();
            mBufWriter.close();
        }
        catch (Throwable e)
        {
            try
            {
                mBufWriter.close();
            }
            catch (Throwable t)
            {
            }
            ;
        }
        return;
    }

}

⌨️ 快捷键说明

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