I have problem with reading text form InputStream connected with the URL.
I’m using Scanner to read text but it looks like there were no text formating.
Here is code
connection = new URL(finalURL).openConnection();
connection.connect();
inStream = connection.getInputStream();
in = new Scanner(inStream);
while(in.hasNextLine()){
line = in.nextLine();
System.out.println(line);
}
I have omitted try catch clause.
The output is some like this:
µtÂ÷BPv§2d
ŐüUŘ}ĎÓăR
While it shoud be like this:
06MAGNA,20121109,0.26,0.27,0.25,0.27,37820
08OCTAVA,20121109,0.73,0.75,0.73,0.73,12244
When I have saved it on disk as txt file and then use Scanner it works fine but via URL it doesn’t. Can anyone help me?
I think its happening because of charset different in the input stream and default charset of scanner. Try passing the
Charsetin theScannerconstructor.EDIT: You can get content encoding using
connection.getContentEncoding().Update your scanner instantiation as:
EDIT1: To deal with
gzipinput stream, useGZIPInputStreamas below: