Files are not resources. If you're creating them, writing them, etc, you shouldn't and probably can't read them as resources. Use new FileInputStream() or new FileReader().
Calling createNewFile() before either new FileOutputStream() or new FileWriter is not only redundant but wasteful.
Applications should not read their own log files, or each other's. This is a job for a database.
Read log file while it is updated
Question
I have server class that have remote object and clients using that remote object for some methods. This object holds log information of the client. However even the text file that holds information of the client is updated by client actions, I cannot read this updated version of the text when client request log until I close the server.
Basically when client rquest statictic it should be written to log file. So I create log file if it does not exist (createLogFile) and then write log information (setLog).
The remote object implementation is following:
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();
}
}
Here is server side:
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();
}
}
}
And here is client side:
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();
}
}
}
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow