android平臺上的文件下載,文件和文件的操作 – 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)url.openConnection(); 
             in=con.getInputStream(); 
             
        reader=new BufferedReader(new InputStreamReader(in)); 
         
        String line; 
        while((line=reader.readLine())!=null){ 
            result.append(line); 
        }    
             
        } catch (MalformedURLException e) { 
            e.printStackTrace(); 
        }catch (IOException e) { 
            e.printStackTrace(); 
        }finally{ 
            try { 
                if(reader!=null) 
                    reader.close(); 
                if(in!=null) 
                    in.close(); 
                 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
             
        } 
        return result.toString(); 
    } 
    /*將文件寫入SD卡
     * @return -1 寫入失敗 0存在 1寫入成功
     * @param urlStr 本機測試是不是ip不是localhost和127.0.0.1,這是指android的IP,電腦是10.0.2.2
     * */ 
     
     
    public int downloadFile(String urlStr,String toPath,String fileName){ 
        try { 
            URL url=new URL(urlStr); 
        URLConnection con=url.openConnection(); 
        InputStream in=con.getInputStream(); 
        FileUtil fileUtil=new FileUtil(); 
        if(fileUtil.isFileExist(toPath+fileName)){ 
             
            return 0; 
        } 
         
        File file=fileUtil.writeToSDFromInputStream(toPath, fileName, in); 
             
        if(file==null){ 
            return -1; 
        } 
         
        } catch (MalformedURLException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
            return -1; 
        } catch (IOException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
            return -1; 
        } 
         
        return 1; 
         
         
         
         
    } 
     
     
     
     

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)url.openConnection(); 
             in=con.getInputStream(); 
             
        reader=new BufferedReader(new InputStreamReader(in)); 
         
        String line; 
        while((line=reader.readLine())!=null){ 
            result.append(line); 
        }    
             
        } catch (MalformedURLException e) { 
            e.printStackTrace(); 
        }catch (IOException e) { 
            e.printStackTrace(); 
        }finally{ 
            try { 
                if(reader!=null) 
                    reader.close(); 
                if(in!=null) 
                    in.close(); 
                 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
             
        } 
        return result.toString(); 
    } 
    /*將文件寫入SD卡
     * @return -1 寫入失敗 0存在 1寫入成功
     * @param urlStr 本機測試是不是ip不是localhost和127.0.0.1,這是指android的IP,電腦是10.0.2.2
     * */ 
     
     
    public int downloadFile(String urlStr,String toPath,String fileName){ 
        try { 
            URL url=new URL(urlStr); 
        URLConnection con=url.openConnection(); 
        InputStream in=con.getInputStream(); 
        FileUtil fileUtil=new FileUtil(); 
        if(fileUtil.isFileExist(toPath+fileName)){ 
             
            return 0; 
        } 
         
        File file=fileUtil.writeToSDFromInputStream(toPath, fileName, in); 
             
        if(file==null){ 
            return -1; 
        } 
         
        } catch (MalformedURLException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
            return -1; 
        } catch (IOException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
            return -1; 
        } 
         
        return 1; 
         
         
         
         
    } 
     
     
     
     

作者“藤之內de學習”
 

發佈留言