1

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?)?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Shrek Ding
  • 45
  • 6

2 Answers2

1

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”?.

Community
  • 1
  • 1
Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
  • Ravinder, thanks a lot, it is useful, especially the Apache implementation of tail. Emm..., is there any reference about getting file content continously from remote windows? – Shrek Ding Jun 17 '12 at 12:59
  • what I mean is on Linux we have _tail -f_, but on windows, have we to implement this function ourselves? – Shrek Ding Jun 17 '12 at 13:05
  • @ShrekDing I have updated my answer with other references to windows equivalent versions of `tail -f`. – Ravinder Reddy Jun 17 '12 at 17:22
0

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

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75