简体   繁体   中英

Is that possible to get file content continuously from remote server

I know locally it is possible to get file content line by line. just like

Scanner s = new Scanner(new File("D:\\Users\\qding\\Desktop\\spy.log"));
    while (s.hasNextLine()) {
        String line = s.nextLine();
        System.out.println("[Method Server] " + line);
}

Is it possible to get file content from remote (Windows/Linux)?

Also, on the remote, the file was a log file, and it is updated always. So the second question is if is possible to file content continuously (with multiple thread?)?

A1 . Yes it is possible to get file content from remote system, provided it is accessible over any protocol.

A2 . On unix systems, to read content of a file that is changing continuously, we have tail -f ... command. Please refer to File Monitoring . And to read such files over network, you definitely require help of Threads to wait and read the updates. Apache commons has custom tail implementations that may help you.

You can refer to a similar posting on SO, for detailed suggestions and discussions.

Update :
A2 : For windows, there is a third party GUI tool Tail for Win32 , but never I worked on it.
You can also refer to some more suggestions at Windows ... equivalent of “tail -f”? .

Well let me try to answer this,

  1. I am assuming that you have used Socket, to communicate with the Server.

  2. Use socket.shutdownOutput() ("socket" in socket.shutdownOutput() identifies the Server address and port nos) on the client side , as you dont need to write something but need to keep reading from the Server.

  3. If you are handling the Server part also, make sure every request to the Server runs on its own thread.

  4. Now on the client side use this below link to read from the "file" on the server

    http://www.java-samples.com/showtutorial.php?tutorialid=215

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM