Save InputStream to file

Put your problem here if it does not fit any of the other categories.

Save InputStream to file

Postby IHeartAndroid » Fri Oct 02, 2009 6:57 am

Hello,

My question is how can I save an InputStream to a local file ?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. URL u;
  3.  
  4.  
  5.  
  6.                         u = new URL(www.myconnection.com/getinput);
  7.  
  8.  
  9.  
  10.                         HttpClient httpClient = new DefaultHttpClient();
  11.  
  12.                         HttpGet httpGet = new HttpGet(u.toString());
  13.  
  14.                        
  15.  
  16.                        
  17.  
  18.                         HttpResponse response = null;
  19.  
  20.                         response = httpClient.execute(httpGet);
  21.  
  22.  
  23.  
  24.                         HttpEntity entity = null;                      
  25.  
  26.                        
  27.  
  28.                         if (response != null)
  29.  
  30.                                 entity = response.getEntity();
  31.  
  32.  
  33.  
  34.  
  35.  
  36.                         InputStream myInputStream = entity.getContent();
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.                          // How can i save the myInputStream to test.xml ?
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4



Moreover even if I figured out how to save the Stream, I still would not be sure where I can find the file on my device .. ?

Can anyone help me please ?
User avatar
IHeartAndroid
Junior Developer
Junior Developer
 
Posts: 24
Joined: Wed Jul 08, 2009 8:51 am

Top

Postby umair » Mon Oct 05, 2009 6:32 am

You can try following code to save inputstream's data to file:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. try {
  2.  
  3.                 InputStream is = null; // Your stream here (myInputStream)
  4.  
  5.        
  6.  
  7.                 File root = Environment.getExternalStorageDirectory();
  8.  
  9.                 String localFilePath = root.getPath() + "/myFile.xml";
  10.  
  11.                
  12.  
  13.                 FileOutputStream fos = new FileOutputStream(localFilePath, false);
  14.  
  15.                 OutputStream os = new BufferedOutputStream(fos);
  16.  
  17.                
  18.  
  19.                 byte[] buffer = new byte[1024];
  20.  
  21.                 int byteRead = 0;
  22.  
  23.                
  24.  
  25.                 while ((byteRead = is.read(buffer)) != -1) {
  26.  
  27.                         os.write(buffer, 0, byteRead);
  28.  
  29.                 }
  30.  
  31.                 fos.close();
  32.  
  33.         } catch (Exception e) {
  34.  
  35.                 e.printStackTrace();
  36.  
  37.         }
  38.  
  39.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
Muhammad Umair
User avatar
umair
Developer
Developer
 
Posts: 28
Joined: Thu Feb 26, 2009 1:00 pm
Location: Lahore, Pakistan

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 11 guests