2

all:

I wonder how to quickly read the last line of an online file, such as "http://www.17500.cn/getData/ssq.TXT",

I know the RandomAccessFile class, but it seems that it can only read the local files. Any suggestion ?? TKS in advance.

Jason Zhu
  • 71
  • 1
  • 6

1 Answers1

3

You'll have to read through the whole reader, and only keep the last line:

String line;
String lastLine = null;
while ((line = reader.readLine()) != null) {
    lastLine = line;
}

EDIT: as Joachim says in his comment, if you know that the last line will never be longer than (for example) 500 bytes, you could set the Range header in your HTTP request to -500, and thus only download the last 500 bytes. The same algorithm as above could be used.

I don't know, however, if it would deal correctly a stream starting in the middle of a multi-byte encoded character if the encoding is multi-byte (like UTF-8). With ASCII or ISO-8859-1, you won't have any problem.

Also note that the server is not forced to honor the range request, and could retur the whole file.

httpConnection.setRequestProperty("Range","-500");
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 2
    If you can make an educated guess on the maximum size of the last line, then you might be able to be much more efficient than that, especially for huge files. – Joachim Sauer Feb 10 '12 at 08:32
  • en. Is this the ONLY way ? If the file is very large, this maybe will waste too much time – Jason Zhu Feb 10 '12 at 08:45
  • See my edit of the answer. And see http://tools.ietf.org/html/rfc2616#section-14.35.1 for more details. – JB Nizet Feb 10 '12 at 08:49
  • 1
    HTTP allows partial downloads, which are used by download managers. So you could load the last n-Bytes (@JoachimSauer = educated guess). If there is at least one linebreak in the data, your done. Get all the Chars after the last linebreak. If not, get the next chunk of data from the end of file and check for linebreak. And so on. Haven't done this in java. So you have to google. Maybe theres a download manager with example code. – Andreas Feb 10 '12 at 08:51
  • Maybe this is a not good idea, cause DownloadManager is available for 2.3. What about the 2.1 & 2.2 ? – Jason Zhu Feb 10 '12 at 09:34
  • Note regarding the UTF-8 problem: UTF-8 is designed in a way that always allows you to find the start of the next character, even if you start a stream in the middle of a multi-byte character. – Joachim Sauer Feb 10 '12 at 09:57
  • @JasonZhu: what DownloadManager and what version numbers are you talking about? Is this about Android? – Joachim Sauer Feb 10 '12 at 09:57
  • @JoachimSauer: But will an InputStreamReader discard the first byte, or throw an exception, or return a line starting with an "unknown character" in this case? – JB Nizet Feb 10 '12 at 10:23
  • @JBNizet: good question, the default behaviour is probably to replace it with one or more "undecodable character" symbols, but you can change it by creating your own `CharsetDecoder`. – Joachim Sauer Feb 10 '12 at 11:20
  • @JoachimSauer: yes, on android – Jason Zhu Feb 13 '12 at 01:32