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

📄 logmanager.java

📁 RemoteWAP is a Remote Administration Tool for any Operating System that can support the Java Virtual
💻 JAVA
字号:
/*
 *  LogManager.java
 *
 *  Copyright (C) 2004 Jay Scott
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program (gpl.txt); if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package viewer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *  The Log Manager class manages all operations to do with the log files. This
 *  includes Deleting, Creating, Appending and Reading the files.
 *
 *@author     Jay Scott
 *@created    July 21, 2004
 *@version    0.4
 */
public class LogManager {

    /**
     * This function gets the File Location and Name from the passed in variable
     * (LogName) and checks if the file is there and then the file is deleted
     * from the hard drive.
     * 
     * @param LogName
     *            The name of the log file to delete
     * @return Returns boolean value
     */
    public boolean deleteLogFile(String LogName) {

        boolean success = (new File(LogName)).delete();

        return success;
    }

    /**
     * This method gets the file location from a passed in variable (LogName)
     * and reads the file contents into a BufferReader which is then returned.
     * 
     * @param LogName
     *            The name of the log file to delete
     * @return The contains for the log file
     */
    public String readLogFile(String LogName) {

        File tmpFile;
        String strContent = "";

        tmpFile = new File(LogName);
        
        /* Read each line of the file into a temp variable */
        try {
            BufferedReader in = new BufferedReader(new FileReader(tmpFile));
            String str;
            while ((str = in.readLine()) != null) {
                strContent = strContent + str + "\n";
            }
            in.close();
        } catch (IOException e) {
            return strContent;
        }

        return strContent;
    }

    /**
     * This method gets a file location (logPath) and data (fileData). It checks
     * if there is already a file with the same name. If there is, it adds the
     * new text to that file else it creates a new file and writes the data to
     * it. The naming system I have used is to grab the date and the time then
     * add .log to the end of the formatted date.
     * 
     * @param fileData
     *            This holds the information that has to be written to disk.
     * @param logPath
     *            Description of the Parameter
     */
    public void writeLogFile(String fileData, String logPath) {

        Format DateFormat;

        /* Get the todays date */
        Date date = new Date();

        DateFormat = new SimpleDateFormat("dd-MMM-yy");
        String s = DateFormat.format(date);

        /* Check if the file exists */
        boolean fileExists = (new File(System.getProperty("user.dir")
                + File.separatorChar + logPath + File.separatorChar + s
                + ".log")).exists();

        String fileLog = System.getProperty("user.dir") + File.separatorChar
                + logPath + File.separatorChar + s + ".log";

        try {
        	/* If fileExists is true */
            if (fileExists) {
                BufferedWriter out = new BufferedWriter(new FileWriter(fileLog,
                        true));
                out.write(fileData);
                out.close();

            } else {
                BufferedWriter out = new BufferedWriter(new FileWriter(fileLog));
                out.write(fileData);
                out.close();
            }

        } catch (IOException e) {
        }
    }
}

⌨️ 快捷键说明

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