Android 文件操作工具类

package cn.ibabyzone.library;

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

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

//用户数据读写函数
//存在SD卡时数据从SD卡读写不存在时通过DATA目录读写
public class FileUtils
{
	private String SDPATH;
	private int FILESIZE = 1;

	public String getSDPATH()
	{
		return SDPATH;
	}

	public FileUtils(Activity thisActivity)
	{
		if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
		{
			SDPATH = Environment.getExternalStorageDirectory() + "/ibabyzone/";
			
		}
		else
		{
			SDPATH= thisActivity.getFilesDir().toString() + "/ibabyzone/";
		}
			
	}

	/**
	 * 在SD卡上创建文件
	 */
	public File creatSDFile(String fileName) throws IOException
	{
		File file = new File(SDPATH + fileName);
		file.createNewFile();
		return file;
	}
	
	public void delFolder(String folderPath) {
        try {
                delAllFile(folderPath); //删除完里面所有内容
                String filePath = folderPath;
                filePath = filePath.toString();
                java.io.File myFilePath = new java.io.File(filePath);
                myFilePath.delete(); //删除空文件夹

       }
        catch (Exception e) {
                System.out.println("删除文件夹操作出错");
                e.printStackTrace();

       }
	}

	/**
	 * 删除文件夹里面的所有文件
	 * @param path String 文件夹路径 如 c:/fqf
	 */
	public void delAllFile(String path) {
        File file = new File(path);
        if (!file.exists()) {
                return;
        }
        if (!file.isDirectory()) {
       return;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
                if (path.endsWith(File.separator)) {
                        temp = new File(path + tempList[i]);
                }
                else {
                        temp = new File(path + File.separator + tempList[i]);
                }
                if (temp.isFile()) {
                        temp.delete();
                }
                if (temp.isDirectory()) {
                        delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件
                        delFolder(path+"/"+ tempList[i]);//再删除空文件夹
                }
        }
	} 

	/**
	 * 在SD卡上创建目录
	 */
	public File creatSDDir(String dirName)
	{
		File dir = new File(SDPATH + dirName);
		dir.mkdir();
		return dir;
	}

	/**
	 * 判断SD卡上的文件夹是否存在
	 */
	public boolean isFileExist(String fileName)
	{
		File file = new File(SDPATH + fileName);
		return file.exists();
	}
	
	public long file_time(String fileName)
	{
		File file = new File(SDPATH + fileName);
		return file.lastModified();
	}

	/**
	 * 将一个InputStream里面的数据写入到SD卡中
	 */
	public File write2SDFromInput(String path, String fileName,InputStream input)
	{
		File file = null;
		OutputStream output = null;
		try
		{
			creatSDDir(path);
			file = creatSDFile(path + fileName);
			output = new FileOutputStream(file);
			byte[] buffer = new byte[FILESIZE];
			while ((input.read(buffer)) != -1)
			{
				output.write(buffer);
			}
			output.flush();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				output.close();
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		return file;
	}
	
	public static void deleteFile(String fileStr) {
		File file = new File(fileStr);
		if (file.exists()) { // 判断文件是否存在
			if (file.isFile()) { // 判断是否是文件
				file.delete(); // delete()方法 你应该知道 是删除的意思;
			}
			file.delete();
		}
	}
	
	public void delFile(String path) {
		String str = SDPATH+path;
		File file = new File(str);
		if (file.exists()) {
			file.delete();
		}
	}
	
	public int CopyFileToFile(File fromFile, File toFile) {

		try {
			InputStream fosfrom = new FileInputStream(fromFile);
			OutputStream fosto = new FileOutputStream(toFile);
			byte bt[] = new byte[1024];
			int c;
			while ((c = fosfrom.read(bt)) > 0) {
				fosto.write(bt, 0, c);
			}
			fosfrom.close();
			fosto.close();
			return 0;

		} catch (Exception ex) {
			return -1;
		}
	}
	
	public File CopyFileRomToSD(String romfile){
		if(!android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
		{
			return null;
			
		}
		
		File rom = new File(romfile);
		File sd = new File(Environment.getExternalStorageDirectory() + "/ibabyzone/"+romfile);
		
		if(CopyFileToFile(rom,sd) == 0){
			return sd;
		}
		
		return null;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值