Android--SD卡上的创建、读取、写入文件及目录

package com.han.jiang.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.Properties;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;

public class FileUtils {

    private static int FILESIZE = 4 * 1024;   
    public static String APP_BASE_DIR = "OA/";
    public static String SENDFILE_DIR = APP_BASE_DIR+"SENDFILE/";  
    public static String NOTICE_DIR = APP_BASE_DIR+"NOTICE/";  
    public static String APK_DIR = APP_BASE_DIR+"APK/";  
    public static String getSDPATH(){  
        return Environment.getExternalStorageDirectory() + "/";  
    }  
    public static String getSendFileDir(){
    	 return getSDPATH()+ SENDFILE_DIR ;
    }
    public static String getNoticeFileDir(){
    	return getSDPATH()+ NOTICE_DIR ;
    }
    public static String getAPKFileDir(){
    	return getSDPATH()+ APK_DIR ;
    }
    /**  
     * 在SD卡上创建文件  
     * @param fileName  
     * @return  
     * @throws IOException  
     */  
    public static  File createSDFile(String fileName) throws IOException{  
        File file = new File(getSDPATH() + fileName);  
        file.createNewFile();  
        return file;  
    }  
      
    /**  
     * 在SD卡上创建目录  
     * @param dirName  
     * @return  
     */  
    public static  File createSDDir(String dirName){  
        File dir = new File(getSDPATH() + dirName);  
        dir.mkdirs();  
        return dir;  
    }  
      
    /**  
     * 判断SD卡上的文件夹是否存在  
     * @param fileName  
     * @return  
     */  
    public static boolean isFileExist(String fileName){  
        File file = new File(getSDPATH() + fileName);  
        return file.exists();  
    }  
      
    /**  
     * 将一个InputStream里面的数据写入到SD卡中  
     * @param path  
     * @param fileName  
     * @param input  
     * @return  
     */  
    public static File write2SDFromInput(String path,String fileName,InputStream input){  
        File file = null;  
        OutputStream output = null;  
        try {  
            createSDDir(path);
            if(input == null)
            	return null;
            file = createSDFile(path + fileName);
            output = new FileOutputStream(file);  
            byte[] buffer = new byte[FILESIZE];
            int len ;
            while((len = input.read(buffer)) != -1){  
                output.write(buffer,0,len);  
            }  
            output.flush();  
        }   
        catch (Exception e) {  
            e.printStackTrace();  
        }  
        finally{  
            try {  
                output.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
        return file;  
    }  
    
    public static Intent getPdfFileIntent(String path){  
        Intent i = new Intent(Intent.ACTION_VIEW);  
        i.addCategory(Intent.CATEGORY_DEFAULT);  
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );  
        Uri uri = Uri.fromFile(new File(path));  
        i.setDataAndType(uri, "application/pdf");  
        return i;  
    }  
    public static Intent getFileIntent(String filePath,String dataAndType )
    {
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.addCategory(Intent.CATEGORY_DEFAULT);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      Uri uri = Uri.fromFile(new File(filePath ));
      intent.setDataAndType(uri,dataAndType);
      return intent;
    }
    public static String getDataAndType(Properties prop,String filePath){
    	if(prop == null)
    		throw new RuntimeException("没有定义配置文件");
    	String fileExtName = getFileExtName(filePath);
    	return prop.getProperty(fileExtName);
    }
    public static Intent getWordFileIntent(String param )
    {
      Intent intent = new Intent(Intent.ACTION_VIEW);
      intent.addCategory(Intent.CATEGORY_DEFAULT);
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      Uri uri = Uri.fromFile(new File(param ));
      intent.setDataAndType(uri,"application/msword");
      return intent;
    }
    public static String getSendFileWordDir(){
    	return getSdCardRootPath()+SENDFILE_DIR;
    }
    public static String getSdCardRootPath(){
    	 //得到当前外部存储设备的目录( /SDCARD )  
       return Environment.getExternalStorageDirectory() + "/";  
    }
    public static String getFileExt(String fileName){
    	return fileName.substring(fileName.lastIndexOf("."));
    }
    public static String getFileExtName(String fileName){
    	return fileName.substring(fileName.lastIndexOf(".")+1);
    }
    public static String getDocFilePath(Map<String,String> detail){
		return getSendFileDir()+getDocFileName(detail);
	}
    public static String getDocFileName(Map<String,String> detail){
    	return detail.get("filesensorid")+"_"+detail.get("censorstate")+".doc";
    }
    public static String getAttFileName(Map<String,String> detail){
    	String fileExt = FileUtils.getFileExt(detail.get("annexid"));
    	return detail.get("filesensorid")+"_"+detail.get("censorstate")+"_att"+fileExt;
    }
    public static String getAttFilePath(Map<String,String> detail){
    	return getSendFileDir()+getAttFileName(detail);
    }
    public static String getAPKFilePath(String fileName){
    	return getAPKFileDir()+fileName;
    }
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值