GZIPInputStream, читающий линию за линией

import socket
socket.gethostbyname(socket.gethostname())

Это не будет всегда работать (возвраты 127.0.0.1 на машинах, имеющих имя узла в /etc/hosts как 127.0.0.1), паллиатив был бы тем, что показывает gimel, используйте socket.getfqdn() вместо этого. Конечно, Вашей машине нужно разрешимое имя узла.

77
задан h22 1 February 2013 в 18:10
поделиться

2 ответа

The basic setup of decorators is like this:

InputStream fileStream = new FileInputStream(filename);
InputStream gzipStream = new GZIPInputStream(fileStream);
Reader decoder = new InputStreamReader(gzipStream, encoding);
BufferedReader buffered = new BufferedReader(decoder);

The key issue in this snippet is the value of encoding. This is the character encoding of the text in the file. Is it "US-ASCII", "UTF-8", "SHIFT-JIS", "ISO-8859-9", …? there are hundreds of possibilities, and the correct choice usually cannot be determined from the file itself. It must be specified through some out-of-band channel.

For example, maybe it's the platform default. In a networked environment, however, this is extremely fragile. The machine that wrote the file might sit in the neighboring cubicle, but have a different default file encoding.

Most network protocols use a header or other metadata to explicitly note the character encoding.

In this case, it appears from the file extension that the content is XML. XML includes the "encoding" attribute in the XML declaration for this purpose. Furthermore, XML should really be processed with an XML parser, not as text. Reading XML line-by-line seems like a fragile, special case.

Failing to explicitly specify the encoding is against the second commandment. Use the default encoding at your peril!

135
ответ дан 24 November 2019 в 10:51
поделиться
GZIPInputStream gzip = new GZIPInputStream(new FileInputStream("F:/gawiki-20090614-stub-meta-history.xml.gz"));
BufferedReader br = new BufferedReader(new InputStreamReader(gzip));
br.readLine();

41
ответ дан 24 November 2019 в 10:51
поделиться
Другие вопросы по тегам:

Похожие вопросы: