码迷,mamicode.com
首页 > 编程语言 > 详细

(转)FileUtil.java 工具类

时间:2015-11-02 13:45:16      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

package com.spring.sky.dialog.download;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.DecimalFormat;

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

/**
 * 文件工具类
 * @author spring sky
 *
 */
public class FileUtil {
  /**
   * 获取目录名称
   * @param url
   * @return FileName
   */
  public static String getFileName(String url)
  {
    int lastIndexStart = url.lastIndexOf("/");
    if(lastIndexStart!=-1)
    {
      return url.substring(lastIndexStart+1, url.length());
    }else{
      return new Timestamp(System.currentTimeMillis()).toString();
    }
  }
  /**
   * 判断SD卡是否存在
   * @return boolean
   */
  public static boolean checkSDCard() {
    if (android.os.Environment.getExternalStorageState().equals(
        android.os.Environment.MEDIA_MOUNTED)) {
      return true;
    } else {
      return false;
    }
  }
  
  /**
   * 保存目录目录到目录
   * @param context
   * @return  目录保存的目录
   */
  public static String setMkdir(Context context)
  {
    String filePath = null;
    if(checkSDCard())
    {
      filePath = Environment.getExternalStorageDirectory()+File.separator+"yishuabao"+File.separator+"downloads";
    }else{
      filePath = context.getCacheDir().getAbsolutePath()+File.separator+"yishuabao"+File.separator+"downloads";
    }
    File file = new File(filePath);
    if(!file.exists())
    {
      file.mkdirs();
      Log.e("file", "目录不存在   创建目录    ");
    }else{
      Log.e("file", "目录存在");
    }
    return filePath;
  }
  
  /**
   * 获取路径
   * @return
   * @throws IOException
   */
  public static  String getPath(Context context,String url)
  {
    String path = null;
    try {
      path = FileUtil.setMkdir(context)+File.separator+url.substring(url.lastIndexOf("/")+1);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return path;
  }
  
  /**
   * 获取文件的大小
   * 
   * @param fileSize
   *            文件的大小
   * @return
   */
  public static String FormetFileSize(int fileSize) {// 转换文件大小
    DecimalFormat df = new DecimalFormat("#.00");
    String fileSizeString = "";
    if (fileSize < 1024) {
      fileSizeString = df.format((double) fileSize) + "B";
    } else if (fileSize < 1048576) {
      fileSizeString = df.format((double) fileSize / 1024) + "K";
    } else if (fileSize < 1073741824) {
      fileSizeString = df.format((double) fileSize / 1048576) + "M";
    } else {
      fileSizeString = df.format((double) fileSize / 1073741824) + "G";
    }
    return fileSizeString;
  }
  
}

Author:   spring sky  

Email:   vipa1888@163.com

(转)FileUtil.java 工具类

标签:

原文地址:http://www.cnblogs.com/weidingqiang/p/4929749.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!