- import java.io.File;  
 
- import java.io.FileOutputStream;  
 
- import java.io.IOException;  
 
- import java.io.InputStream;  
 
- import java.io.OutputStream;  
 
- import android.os.Environment;  
 
- import android.os.StatFs;  
 
- import android.util.Log;  
 
-   
 
- public class FileUtil {  
 
-     private static int bufferd = 1024;  
 
-   
 
-     private FileUtil() {  
 
-     }  
 
-   
 
-     
 
-   
 
-     
 
-     public static boolean isSdcardAvailable() {  
 
-         String status = Environment.getExternalStorageState();  
 
-         if (status.equals(Environment.MEDIA_MOUNTED)) {  
 
-             return true;  
 
-         }  
 
-         return false;  
 
-     }  
 
-   
 
-     public static long getSDAllSizeKB() {  
 
-         
 
-         File path = Environment.getExternalStorageDirectory();  
 
-         StatFs sf = new StatFs(path.getPath());  
 
-         
 
-         long blockSize = sf.getBlockSize();  
 
-         
 
-         long allBlocks = sf.getBlockCount();  
 
-         
 
-         return (allBlocks * blockSize) / 1024; 
 
-     }  
 
-   
 
-     
 
-     public static long getSDAvalibleSizeKB() {  
 
-         File path = Environment.getExternalStorageDirectory();  
 
-         StatFs sf = new StatFs(path.getPath());  
 
-         long blockSize = sf.getBlockSize();  
 
-         long avaliableSize = sf.getAvailableBlocks();  
 
-         return (avaliableSize * blockSize) / 1024;
 
-     }  
 
-   
 
-     
 
-     public static boolean isFileExist(String director) {  
 
-         File file = new File(Environment.getExternalStorageDirectory()  
 
-                 + File.separator + director);  
 
-         return file.exists();  
 
-     }  
 
-   
 
-     
 
-     public static boolean createFile(String director) {  
 
-         if (isFileExist(director)) {  
 
-             return true;  
 
-         } else {  
 
-             File file = new File(Environment.getExternalStorageDirectory()  
 
-                     + File.separator + director);  
 
-             if (!file.mkdirs()) {  
 
-                 return false;  
 
-             }  
 
-             return true;  
 
-         }  
 
-     }  
 
-   
 
-     public static File writeToSDCardFile(String directory, String fileName,  
 
-             String content, boolean isAppend) {  
 
-         return writeToSDCardFile(directory, fileName, content, "", isAppend);  
 
-     }  
 
-   
 
-     
 
-     public static File writeToSDCardFile(String directory, String fileName,  
 
-             String content, String encoding, boolean isAppend) {  
 
-         
 
-         File file = null;  
 
-         OutputStream os = null;  
 
-         try {  
 
-             if (!createFile(directory)) {  
 
-                 return file;  
 
-             }  
 
-             file = new File(Environment.getExternalStorageDirectory()  
 
-                     + File.separator + directory + File.separator + fileName);  
 
-             os = new FileOutputStream(file, isAppend);  
 
-             if (encoding.equals("")) {  
 
-                 os.write(content.getBytes());  
 
-             } else {  
 
-                 os.write(content.getBytes(encoding));  
 
-             }  
 
-             os.flush();  
 
-         } catch (IOException e) {  
 
-             Log.e("FileUtil", "writeToSDCardFile:" + e.getMessage());  
 
-         } finally {  
 
-             try {  
 
-                 if(os != null){  
 
-                     os.close();  
 
-                 }  
 
-             } catch (IOException e) {  
 
-                 e.printStackTrace();  
 
-             }  
 
-         }  
 
-         return file;  
 
-     }  
 
-   
 
-     
 
-     public File writeToSDCardFromInput(String directory, String fileName,  
 
-             InputStream input) {  
 
-         File file = null;  
 
-         OutputStream os = null;  
 
-         try {  
 
-             if (createFile(directory)) {  
 
-                 return file;  
 
-             }  
 
-             file = new File(Environment.getExternalStorageDirectory()  
 
-                     + File.separator + directory + fileName);  
 
-             os = new FileOutputStream(file);  
 
-             byte[] data = new byte[bufferd];  
 
-             int length = -1;  
 
-             while ((length = input.read(data)) != -1) {  
 
-                 os.write(data, 0, length);  
 
-             }  
 
-             
 
-             os.flush();  
 
-         } catch (Exception e) {  
 
-             Log.e("FileUtil", "" + e.getMessage());  
 
-             e.printStackTrace();  
 
-         } finally {  
 
-             try {  
 
-                 os.close();  
 
-             } catch (Exception e) {  
 
-                 e.printStackTrace();  
 
-             }  
 
-         }  
 
-         return file;  
 
-     }  
 
-   
 
-     
 
-     public static String getUrlLastString(String url) {  
 
-         String[] str = url.split("/");  
 
-         int size = str.length;  
 
-         return str[size - 1];  
 
-     }  
 
-   
 
- }  
 
下面对代码进行了测试,使用了AndroidJunitTest,当然另外还需要对SDCard查看操作的权限。
1、对android项目的mainfest.xml进行配置,需要注意targetPacket应当与包名保持一致。
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
 
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  
 
- <instrumentation  
 
-         android:name="android.test.InstrumentationTestRunner"  
 
-         android:targetPackage="com.example.mygeneralutil" >  
 
- </instrumentation>  
 
-    
 
- <uses-library android:name="android.test.runner"/>  
 
 
2、简单的测试代码如下:
- import android.test.AndroidTestCase;  
 
- import android.util.Log;  
 
-   
 
- public class FileUtilTest extends AndroidTestCase {  
 
-   
 
-     public void testIsSdcardAvailable() {  
 
-         FileUtil.isSdcardAvailable();  
 
-         Log.e("FileUtil", ""+FileUtil.isSdcardAvailable());  
 
-     }  
 
-   
 
-     public void testGetSDAllSizeKB() {  
 
-         FileUtil.getSDAllSizeKB();  
 
-         Log.e("FileUtil", ""+(float)FileUtil.getSDAllSizeKB()/1024/1024);  
 
-     }  
 
-   
 
-     public void testGetSDAvalibleSizeKB() {  
 
-         FileUtil.getSDAvalibleSizeKB();  
 
-         Log.e("FileUtil", ""+(float)FileUtil.getSDAvalibleSizeKB()/1024/1024);  
 
-     }  
 
-   
 
-     public void testIsFileExist() {  
 
-         FileUtil.isFileExist("example");  
 
-         Log.e("FileUtil", ""+FileUtil.isFileExist("example"));  
 
-     }  
 
-   
 
-     public void testCreateFile() {  
 
-         Log.e("FileUtil", ""+FileUtil.createFile("forexample"));  
 
-     }  
 
-   
 
-     public void testWriteToSDCardFileStringStringStringBoolean() {  
 
-           
 
-         fail("Not yet implemented");  
 
-     }  
 
-   
 
-     public void testWriteToSDCardFileStringStringStringStringBoolean() {  
 
-         FileUtil.writeToSDCardFile("forexample", "123.txt",   
 
-                 "FileUtil.writeToSDCardFile", "utf-8", true);  
 
-     }  
 
-   
 
-     public void testWriteToSDCardFromInput() {  
 
-         fail("Not yet implemented");  
 
-     }  
 
-   
 
-     public void testGetUrlLastString() {  
 
-         fail("Not yet implemented");  
 
-     }  
 
-   
 
- }  
 
 
ANDROID对文件的操作
原文:http://www.cnblogs.com/Free-Thinker/p/4389251.html