文件操作类FileUnits

package com.example.downloadHelper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.os.Environment;
import android.util.Log;

public class FileUnits {
    
    /**
     * 判断文件夹是否存在
     * true表示存在  false表示不存在
     */
    public Boolean CheckPath(String path){
        File file = new File(path);
        return file.exists();
    }
    
    /**
     * 判断文件夹,如不存在则创建文件夹
     */
    public void CreatePath(String path){
        File file = new File(path);
        if(!file.exists()){
            file.mkdir();
        }
    }
    
    /**
     * 判断文件是否存在,如不存在则新建文件
     */
    public File CreateNewFile(String path,String filename) throws IOException{
        File file = new File(path + filename);
        file.createNewFile();
        return file;
    }
    
    /**
     * 保存文件
     */
    
    public File SaveFile(String path,String filename,InputStream is) {
        File file= null;
        FileOutputStream output = null;
        try {
            CreatePath(path);
            file = CreateNewFile(path,filename);
            output = new FileOutputStream(file);
            byte[]bt=new byte[4*1024];

            while (is.read(bt)!=-1) {
               output.write(bt);
            }
            //刷新缓存,
            output.flush();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally{
            try {
                output.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        return file;
    }
    
    /**
     * 保存文件到SD卡
     */
    public File SaveFileToSDCard(String path,String filename,InputStream is) {
        File file = null;
        FileOutputStream output = null;
        try {
            //追加SD位置
            path += Environment.getExternalStorageDirectory()+"/";
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
            
                //创建文件夹
                CreatePath(path);
                //创建文件
                file = CreateNewFile(path,filename);
                output = new FileOutputStream(file);
                byte[]bt=new byte[4*1024];
    
                while (is.read(bt)!=-1) {
                   output.write(bt);
                }
                //刷新缓存,
                output.flush();
            } else {
                Log.e("SD", "SD卡无写权限");
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            try {
                output.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return file;
    }
    
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值