1

I've made I simple function to write from URL to file. Everything works good until out.write(), I mean there's no exception, but it just doesn't write anything to file

Here's code

private boolean getText(String url, String name) throws IOException { if(url!=null){ FileWriter fstream = new FileWriter(PATH+"/"+name+".txt"); BufferedWriter out = new BufferedWriter(fstream); URL _url = new URL(url); int code = ((HttpURLConnection) _url.openConnection()).getResponseCode(); if(code==200){ URLConnection urlConnection = _url.openConnection(); // InputStream in = new BufferedInputStream(urlConnection.getInputStream()); int bytesRead = 0; byte[] buffer = new byte[1024]; while ((bytesRead = in.read(buffer)) != -1) { String chunk = new String(buffer, 0, bytesRead); out.write(chunk); } return true; } out.close(); } return false; } 

Can someone tell me what's wrong please?

4
  • yes it is, and its empty Commented Sep 13, 2011 at 16:26
  • also, all permisions are set and so on - i've already done writing images and thought text will be much easier but im getting real frustrated right now Commented Sep 13, 2011 at 16:27
  • Is there anything being read? (Does the while loop get executed?) Commented Sep 13, 2011 at 16:43
  • read yes, chunk string has a proper value, but ouw.write doesnt write chunk, got no idea why Commented Sep 13, 2011 at 21:58

1 Answer 1

3

Try fstream.flush().

Also out.close() should be called before returning from a function.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.