Download files from the Internet and write them to the SD card. The idea is as follows:
1. Create an HTTP access
Httpurlconnection httpcon = (httpurlconnection) URL. openconnection ()
2. Get an input stream from httpurlconnection
Inputstream httpinputstream = httcon. getinputstream ()
3. parse the stream
Bufferedreader is generally used for obtaining.
Bufferedreader bfreader = new bufferedreader (New inputsreamreader (inputsream in ))
String line = bfreader. Readline ();
4. Create a file and generate the corresponding writer
File filewrite = new file (localpath );
Fileoutputstream fstream = new fileoutputstream (filewrite );
Bufferedwriter bfwriter = new bufferedwriter (New outputstreamwriter (fstream ));
Bfwriter. Writer (line, 0, line. Length ())
5. Remember to close the reader and writer of the stream.
If it is a non-text file, it is best to read and write it by byte.
Finally, you need to define user permissions in Android manifest. xml.
<Uses-Permission Android: Name = "android. Permission. Internet"/>
<Uses-Permission Android: Name = "android. Permission. write_external_storage"/>