Reputation: 1651
i wonder if there is a way to read text file line by line from the end from FTP server. I need to read large file, and I'm interested only in the last few lines.
I am able to read last line from file on my HD with code from this question: Quickly read the last line of a text file?, and I am able too to read file on FTP server line by line with code from there: Java. Read file from FTP but DON'T download it whole.
But in first case function tail uses variable type File, while second code uses variable type InputStream.
When I was looking for way of conversion stream to file, I found that only real way is download content to tmp file (How to convert InputStream to virtual File). But I don't want to download this file, because it may be be very large, as I wrote.
Does anybody know another way to do this?
Upvotes: 1
Views: 1336
Reputation: 719261
It is not possible using the FTP protocol. The protocol only supports fetching the file starting at the beginning. (There is an optional feature that allows an interrupted transfer to be restarted half way through, but it depends on the server sending opaque "markers" that represent possible resumption points. I cannot see how you could use this to skip to the end of an arbitrary file.)
If you are looking at alternatives, consider using HTTP with a combination of HEAD requests, and GET requests with Range headers. (This depends on the server recognizing those requests ...)
Upvotes: 1
Reputation: 2189
It's not possible. But you can read the input stream line by line and remember the only few last lines. If the next line comes, just forget the first read line.
Upvotes: 1