I'm trying to write a piece of code that connects to a web server, grabs a file (of any type) and saves it locally. So far I have, Code: URL u = new URL(args[0]); URLConnection connection = u.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection)connection; InputStream in = connection.getInputStream(); FileOutputStream outputStream = new FileOutputStream("file.txt"); while(in != null) { outputStream.write(in.read()); } outputStream.close(); At the moment this grabs the file passed to it from the command line (e.g. www.google.co.uk). The problem is the end of the stream never seems to be reached, instead a never ending number of ÿ's are added to the end of the file, I've tried changing the Code: while(in != null) bit to Code: while(in != -1) but that doesn't make any difference. Anyone know what I'm doing wrong?
at a guess its getting from the buffer -1 (0xFFFFFFFF in 32bit), but its been cast to char, not used much java lately i'm affraid, so i can't remeber if it will cast properly in an indentically equal too comprasison. Easyest way would be to say while !=0xFF but that would have problems when that chr occoured in the data. but anyway isn't that a in.EOF or in.notEOF?
I coundn't find anything to do with EOF... Anyway, this seems to have fixed it (edit: and it works nicely for other file types ) Code: int next = in.read(); while(next != -1) { outputStream.write((byte)next); next = in.read(); }