package com.wangbb.naruto.utils; import android.content.Context; import android.os.Environment; import android.os.StatFs; import android.text.TextUtils; import android.util.Log; import java.io.ByteArrayOutputStream; 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 java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; /** * �ļ����������� * * @author yuweichen * */ public class FileUtils { private static String TAG = "FileUtils"; /** * д�ı��ļ� ��Androidϵͳ�У��ļ������� /data/data/PACKAGE_NAME/files Ŀ¼�� * * @param context * @param msg */ public static void write(Context context, String fileName, String content) { if (content == null) content = ""; try { FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE); fos.write(content.getBytes()); fos.close(); } catch (Exception e) { e.printStackTrace(); } } /** * ��ȡ�ı��ļ� * * @param context * @param fileName * @return */ public static String read(Context context, String fileName) { try { FileInputStream in = context.openFileInput(fileName); return readInStream(in); } catch (Exception e) { e.printStackTrace(); } return ""; } private static String readInStream(FileInputStream inStream) { try { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[512]; int length = -1; while ((length = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, length); } outStream.close(); inStream.close(); return outStream.toString(); } catch (IOException e) { Log.e(TAG, "FileTest:" + e.getMessage()); } return null; } public static File createFile(String folderPath, String fileName) { File destDir = new File(folderPath); if (!destDir.exists()) { destDir.mkdirs(); } return new File(folderPath, fileName); } public static boolean createFile(String destFileName) { File file = new File(destFileName); if (!file.getParentFile().exists()) { // ���Ŀ���ļ����ڵ�Ŀ¼�����ڣ��򴴽���Ŀ¼ if (!file.getParentFile().mkdirs()) { return false; } } if (destFileName.endsWith(File.separator)) { return false; } // �ж�Ŀ���ļ����ڵ�Ŀ¼�Ƿ���� // ����Ŀ���ļ� if (file.exists()) { return true; } try { if (file.createNewFile()) { return true; } else { return false; } } catch (IOException e) { e.printStackTrace(); return false; } } /** * ���ֻ�дͼƬ * * @param buffer * @param folder * @param fileName * @return */ public static boolean writeFile(byte[] buffer, String folder, String fileName) { boolean writeSucc = false; boolean sdCardExist = Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED); String folderPath = ""; if (sdCardExist) { folderPath = Environment.getExternalStorageDirectory() + File.separator + folder + File.separator; } else { writeSucc = false; } File fileDir = new File(folderPath); if (!fileDir.exists()) { fileDir.mkdirs(); } File file = new File(folderPath + fileName); FileOutputStream out = null; try { out = new FileOutputStream(file); out.write(buffer); writeSucc = true; } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } return writeSucc; } /** * �����ļ�����·����ȡ�ļ��� * * @param filePath * @return */ public static String getFileName(String filePath) { if (TextUtils.isEmpty(filePath)) return ""; return filePath.substring(filePath.lastIndexOf(File.separator) + 1); } public static String getFileName() { String fileNameRandom = getCharacterAndNumber(); return fileNameRandom + ".jpg"; } public static String getCharacterAndNumber() { String rel = ""; SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss"); Date curDate = new Date(System.currentTimeMillis()); rel = formatter.format(curDate); return rel; } /** * �����ļ��ľ���·����ȡ�ļ�������������չ�� * * @param filePath * @return */ public static String getFileNameNoFormat(String filePath) { if (TextUtils.isEmpty(filePath)) { return ""; } int point = filePath.lastIndexOf('.'); return filePath.substring(filePath.lastIndexOf(File.separator) + 1, point); } /** * ��ȡ�ļ��ĺ�׺�� * * @param fileName * �������ļ��� * @return �ļ��ĺ�׺�� ����".3gp" */ public static String getFileSuffixName(String fileName) { String suffixName = null; if (fileName.lastIndexOf(".") > 0) { suffixName = fileName.substring(fileName.lastIndexOf(".")); } return suffixName; } /** * ��ȡ�ļ���ǰ׺ * * @param fileName * �������ļ��� * @return */ public static String getFilePreffixName(String fileName) { String preffixName = null; if (fileName.lastIndexOf(".") > 0) { preffixName = fileName.substring(0, fileName.lastIndexOf(".")); } return preffixName; } /** * ��ȡ�ļ���չ�� * * @param fileName * @return */ public static String getFileFormat(String fileName) { if (TextUtils.isEmpty(fileName)) return ""; int point = fileName.lastIndexOf('.'); return fileName.substring(point + 1); } /** * ��ȡ�ļ���С * * @param filePath * @return */ public static long getFileSize(String filePath) { long size = 0; File file = new File(filePath); if (file != null && file.exists()) { size = file.length(); } return size; } /** * ��ȡ�ļ���С * * @param size * �ֽ� * @return */ public static String getFileSize(long size) { if (size <= 0) return "0"; java.text.DecimalFormat df = new java.text.DecimalFormat("##.##"); float temp = (float) size / 1024; if (temp >= 1024) { return df.format(temp / 1024) + "M"; } else { return df.format(temp) + "K"; } } /** * ת���ļ���С * * @param fileS * @return B/KB/MB/GB */ public static String formatFileSize(long fileS) { java.text.DecimalFormat df = new java.text.DecimalFormat("#.00"); String fileSizeString = ""; if (fileS < 1024) { fileSizeString = df.format((double) fileS) + "B"; } else if (fileS < 1048576) { fileSizeString = df.format((double) fileS / 1024) + "KB"; } else if (fileS < 1073741824) { fileSizeString = df.format((double) fileS / 1048576) + "MB"; } else { fileSizeString = df.format((double) fileS / 1073741824) + "G"; } return fileSizeString; } /** * ��ȡĿ¼�ļ���С * * @param dir * @return */ public static long getDirSize(File dir) { if (dir == null) { return 0; } if (!dir.isDirectory()) { return 0; } long dirSize = 0; File[] files = dir.listFiles(); for (File file : files) { if (file.isFile()) { dirSize += file.length(); } else if (file.isDirectory()) { dirSize += file.length(); dirSize += getDirSize(file); // �ݹ���ü���ͳ�� } } return dirSize; } public static ArrayList<String> getDirFileName(File dir) { if (dir == null) { return null; } if (!dir.isDirectory()) { return null; } ArrayList<String> list = new ArrayList<String>(); File[] files = dir.listFiles(); for (File file : files) { String fileName = file.getName(); list.add(fileName); } return list; } /** * ��ȡĿ¼�ļ����� * * @param f * @return */ public long getFileList(File dir) { long count = 0; File[] files = dir.listFiles(); count = files.length; for (File file : files) { if (file.isDirectory()) { count = count + getFileList(file);// �ݹ� count--; } } return count; } public static byte[] toBytes(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); int ch; while ((ch = in.read()) != -1) { out.write(ch); } byte buffer[] = out.toByteArray(); out.close(); return buffer; } /** * ����SD����ʣ��ռ� * * @return ����-1��˵��û�а�װsd�� */ public static long getFreeDiskSpace() { String status = Environment.getExternalStorageState(); long freeSpace = 0; if (status.equals(Environment.MEDIA_MOUNTED)) { try { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); freeSpace = availableBlocks * blockSize / 1024; } catch (Exception e) { e.printStackTrace(); } } else { return -1; } return (freeSpace); } /** * ����ļ��Ƿ���� * * @param name * @return */ public static boolean checkFileExists(String name) { boolean status; if (!name.equals("")) { // File path = Environment.getExternalStorageDirectory(); File newPath = new File(name); status = newPath.exists(); } else { status = false; } return status; } /** * �½�Ŀ¼ * * @param directoryName * @return */ public static boolean createDirectory(String directoryName) { boolean status; if (!directoryName.equals("")) { File path = Environment.getExternalStorageDirectory(); File newPath = new File(path.toString() + directoryName); status = newPath.mkdir(); status = true; } else status = false; return status; } /** * ����Ƿ�װSD�� * * @return */ public static boolean checkSaveLocationExists() { String sDCardStatus = Environment.getExternalStorageState(); boolean status; if (sDCardStatus.equals(Environment.MEDIA_MOUNTED)) { status = true; } else status = false; return status; } /** * ɾ��Ŀ¼(������Ŀ¼��������ļ�) * * @param fileName * @return */ public static boolean deleteDirectory(String fileName) { boolean status; SecurityManager checker = new SecurityManager(); if (!fileName.equals("")) { // File path = Environment.getExternalStorageDirectory(); File newPath = new File(fileName); checker.checkDelete(newPath.toString()); if (newPath.isDirectory()) { String[] listfile = newPath.list(); // delete all files within the specified directory and then // delete the directory try { for (int i = 0; i < listfile.length; i++) { File deletedFile = new File(newPath.toString() + "/" + listfile[i].toString()); deletedFile.delete(); } newPath.delete(); Log.i(TAG, "DirectoryManager deleteDirectory:" + fileName); status = true; } catch (Exception e) { e.printStackTrace(); status = false; } } else status = false; } else status = false; return status; } /** * ɾ���ļ� * * @param fileName * @return */ public static boolean deleteFile(String fileName) { boolean status; SecurityManager checker = new SecurityManager(); if (!fileName.equals("")) { // File path = Environment.getExternalStorageDirectory(); File newPath = new File(fileName); checker.checkDelete(newPath.toString()); if (newPath.isFile()) { try { Log.i(TAG, "DirectoryManager deleteDirectory:" + fileName); newPath.delete(); status = true; } catch (SecurityException se) { se.printStackTrace(); status = false; } } else status = false; } else status = false; return status; } /* * ��һ��InputSteam���������д�뵽SD���� */ public static File write2SDFromInput(String path, InputStream input) { File file = new File(path); OutputStream output = null; try { output = new FileOutputStream(file); byte buffer[] = new byte[4 * 1024]; while ((input.read()) != -1) { output.write(buffer); } output.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { output.close(); } catch (Exception e) { e.printStackTrace(); } } return file; } }