从网上下载文件,写入sd卡.思路如下:
1.创建一个http访问
HttpURLConnection httpcon=(HttpURLConnection)url.openConnection()
2.从HttpURLConnection中获取一个输入流
InputStream httpInputStream=httcon.getInputStream()
3.解析流
一般用BufferedReader来获取.
BufferedReader bfReader=new BufferedReader(new InputSreamReader(InputSream in))
String line=bfReader.readLine();
4.创建文件,生成对应的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.最后记得关闭流的reader和writer
如果是非文本文件的话,最好还是按字节来读取和写入.
最后,需要注意的是,要在android manifest.xml中定义用户权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>