更新时读取日志文件

时间:2014-03-15 15:09:37

标签: java logging rmi readfile

我有一个具有远程对象的服务器类和使用该远程对象的客户端用于某些方法。该对象保存客户端的日志信息。但是,即使保存客户端信息的文本文件也会被客户端操作更新,但在关闭服务器之前,我无法在客户端请求日志时读取此文本的更新版本。

基本上当客户端rquest statictic时,它应该写入日志文件。所以我创建日志文件(如果它不存在(createLogFile)然后写日志信息(setLog)。

远程对象实现如下:

public class DictionaryImp extends RemoteServer implements Dictionary {
static FileOutputStream log;
    public DictionaryImp(){
        super();        
    }
public String statictics() {
        // TODO Auto-generated method stub
        try {
            setLog("Statistic", this.getClientHost());
        } catch (ServerNotActiveException e1) {
            // TODO Auto-generated catch block
            System.err.println("Client is not active!!");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       //some statistic    

        return msg;
    }

public String log() throws RemoteException {

        try {
            setLog("Log", this.getClientHost());
        } catch (ServerNotActiveException e1) {
            // TODO Auto-generated catch block
            System.err.println("Client is not active!!");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String msg = "";
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader
                    (getClass().getClassLoader().getResourceAsStream(this.getClientHost()+".txt")));
            String line = null;

            while((line = reader.readLine()) != null){
                msg += line + "\n";
            }
            reader.close();
        } catch (ServerNotActiveException | IOException e) {
            // TODO Auto-generated catch block
            System.err.println("Client is not active");
        }

        return msg;
    }

private static void  createLogFile(String logFileName){
        File logFile = null;
        logFile = new File(logFileName);

        if(!logFile.exists())
            try {
                logFile.createNewFile();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                System.err.println("Log file couldn't created!!");
            }
        try {
            log = new FileOutputStream(logFile, true);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            System.err.println("Log file not found!!");
        }       
    }

    private static void setLog(String event, String ip) throws IOException{
        createLogFile("Resources/"+ip+".txt");
        String time = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(Calendar.getInstance().getTime());
        String msg = ip + "\t " +event + "\t " + time + "\n";
        try {
            log.write(msg.getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.err.println("Log file not found!!");
        }
        log.close();
    }
}

这是服务器端:

import java.rmi.registry.Registry;  
import java.rmi.registry.LocateRegistry;  
import java.rmi.Remote;
import java.rmi.RemoteException;  
import java.rmi.server.UnicastRemoteObject;  

public class ServerRMI {  

    public static void main(String args[]) {  

    try {  
        Dictionary obj = new DictionaryImp();  
       Dictionary stub = (Dictionary) UnicastRemoteObject.exportObject( obj, 9765);  

        // Bind the remote object's stub in the registry  
       Registry registry = LocateRegistry.createRegistry(9765); //your line 23  
       registry.rebind("Dictionary", stub); //your line 24  

       System.err.println("Server ready");  
    } catch (Exception e) {  
        System.err.println("Server exception: " + e.toString());  
        e.printStackTrace();  
    }  
    }  
}

这是客户方:

import java.rmi.registry.LocateRegistry;  
import java.rmi.registry.Registry;  

public class ClientRMI {  

    private ClientRMI() {}  

    public static void main(String[] args) {  

        String host = (args.length < 1) ? null : args[0];  
        try {  
            Registry registry = LocateRegistry.getRegistry("localhost",9765);  
            Dictionary stub = (Dictionary) registry.lookup("Dictionary");  
            System.out.println(stub.statictics());
            System.out.println(stub.log());
        } catch (Exception e) {  
            System.err.println("Client exception: " + e.toString());  
            e.printStackTrace();  
        }  
    }  
} 

1 个答案:

答案 0 :(得分:0)

  1. 文件不是资源。如果您正在创建它们,编写它们等,那么您不应该将它们作为资源读取。使用新的FileInputStream()或新的FileReader()。

  2. 在新FileOutputStream()或新FileWriter之前调用createNewFile()不仅多余而且浪费。

  3. 应用程序不应该读取自己的日志文件或彼此的日志文件。这是数据库的工作。

相关问题