当前位置:操作系统 > 安卓/Android >>

android平台上的文件下载,文件和文件的操作

android平台上对文件的操作和java基本上是相同的
注意用Environment.getExternalStorageDirectory()+"/"获得的SD目录最好
Java代码 
package duohuo.teng.net; 
 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
 
import android.os.Environment; 
 
public class FileUtil { 
 
    private String SDPATH; 
 
    public FileUtil() { 
        //获得SD卡的路劲 
        SDPATH=Environment.getExternalStorageDirectory()+"/";    
    } 
     
     
    /*创建文件
     * 
     * */ 
    public File createFile(String fileName) throws IOException{ 
    File file=new File(SDPATH+fileName); 
    file.createNewFile(); 
        return file; 
    } 
    /*创建文件夹
     * 
     * */ 
    public File createDirs(String dirName){ 
        File file=new File(SDPATH+dirName); 
        Boolean b=file.mkdirs(); 
        System.out.println(b); 
        return file; 
    } 
    /*判断文件(夹)是否存在
     * 
     * */    
    public boolean isFileExist(String path){ 
        return new File(SDPATH+path).exists(); 
         
    } 
    public File writeToSDFromInputStream(String path,String fileName,InputStream in){ 
        File file=null; 
        OutputStream out = null; 
         
        createDirs(path); 
        try { 
             path=path+"/"+fileName; 
            System.out.println(path); 
        file=createFile(path); 
        out=new FileOutputStream(file); 
        byte[] buffer=new byte[4*1024]; 
        while(in.read(buffer)!=-1){ 
            out.write(buffer); 
 
        } 
         
        } catch (IOException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        }finally{ 
            try { 
                if(out!=null) 
                out.close(); 
                if(in!=null) 
                in.close(); 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
             
             
        } 
         
         
        return file; 
    } 
    /*delete file
     * @return 0 文件不存在 1删除成功
     * */ 
    public int deleteFile(String fileAllName){ 
        File file=new File(SDPATH+fileAllName); 
    if(isFileExist(file.getPath())){ 
        return 0; 
    } 
     
    file.delete(); 
    return 1; 
 
         
         
         
         
         
    } 
     

下面是下载
Java代码 
package duohuo.teng.net; 
 
import java.io.BufferedReader; 
import java.io.File; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 
 
 
public class HttpDownloadUtil { 
 
     
    /*将文件保存为String
     * @param urlStr 本机测试是不是ip不是localhost和127.0.0.1,这是指android的IP,电脑是10.0.2.2
     * */ 
    public String downloadString(String urlStr){ 
        StringBuffer result=new StringBuffer();; 
        URL url; 
        InputStream in = null; 
        BufferedReader reader=null; 
        try { 
             url=new URL(urlStr); 
             HttpURLConnection con=(HttpURLConnection

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,