2011-09-13 71 views
1

我做了一個簡單的函數來從URL寫入文件。一切正常,直到好out.write(),我的意思是有沒有例外,但它只是不寫什麼到文件Android寫入文件

這裏的代碼

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; 
} 

有人能告訴我什麼是錯的嗎?

+0

是創建的文件? – Otra

+0

是的,它的空 – krp

+0

也是,所有的權限都設置好了,等等 - 我已經完成了寫圖片和思想文本將會更容易,但即時得到真正的沮喪 – krp

回答

3

嘗試fstream.flush()

還應該在函數返回之前調用out.close()