I am reading an HTML file and saving it in a string. I wanted to read the length (int len = con.getcontentlength()) and put char charArray=new char(len) but the result of getContentLength is always -1.
So I fix the charArray but if I put
char[] charArray=new char[1] or
char[] charArray=new char[512] or
char[] charArray=new char[1024]
it always works.
I don't understand why.
public static void main(String[] args) throws Exception
{
String name="AAAA";
URL url = new URL("http:...");
URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int numCharsRead;
char[] charArray = new char[1];
StringBuffer sb = new StringBuffer();
while ((numCharsRead = isr.read(charArray)) > 0)
sb.append(charArray, 0, numCharsRead);
String htmlString = sb.toString();