編輯:關於Android編程
為了以後其他項目中方便使用,提取之前項目中的一些通用的類,分享給大家。該類為文件簡單操作輔助類。
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * 文件簡單操作輔助類 * @author * */ public class FILE { /** * 文件是否存在 * @param filePathName * @return */ public static boolean isExist(String filePathName) { File file = new File(filePathName); return (!file.isDirectory() && file.exists()); } public static boolean isDirExist(String filePathName) { if(!filePathName.endsWith("/")) filePathName +="/"; File file = new File(filePathName); return (file.isDirectory() && file.exists()); } /** * 獲取路徑,不帶文件名,末尾帶'/' * @param filePathName * @return */ public static String getPath(String filePathName) { try { return filePathName.substring(0, filePathName.lastIndexOf('/') + 1); } catch (Exception e) { return ""; } } /** * 獲取目錄的名稱 注意:只能獲取如:/aaaa/ssss/ 或 /aaaa/dsddd * @param filePathName * @return */ public static String getDirPathName(String filePathName) { try { if(filePathName.endsWith("/")) filePathName = filePathName.substring(0, filePathName.lastIndexOf('/')); return filePathName.substring(filePathName.lastIndexOf("/") + 1, filePathName.length()); } catch (Exception e) { return ""; } } /** * 獲取文件名,帶後綴 * @param filePathName * @return */ public static String getName(String filePathName) { try { return filePathName.substring(filePathName.lastIndexOf('/') + 1); } catch (Exception e) { return ""; } } /** * 獲取文件名,不帶後綴 * @return */ public static String getNameNoPostfix(String filePathName) { try { return filePathName.substring(filePathName.lastIndexOf('/') + 1, filePathName.lastIndexOf('.')); } catch (Exception e) { return ""; } } /** * 重命名 * @param filePathName * @param newPathName */ public static void rename(String filePathName, String newPathName) { if(isNullString(filePathName)) return ; if(isNullString(newPathName)) return ; FILE.delete(newPathName); //liuwp 20120830 新名稱對應的文件可能已經存在,先刪除 File file = new File(filePathName); File newFile = new File(newPathName); file.renameTo(newFile); } /** * 刪除文件 */ public static void delete(String filePathName) { if(isNullString(filePathName)) return ; File file = new File(filePathName); if (file.isFile() && file.exists()) { boolean flag = file.delete(); LOG.I("LOG", "filePathName reset:"+filePathName+" flag:"+flag); } } /** * 創建目錄,整個路徑上的目錄都會創建 * @param path */ public static void createDir(String path) { File file = new File(path); if (!file.exists()) { file.mkdirs(); } } /** 嘗試創建空文件 *
如果文件已經存在不操作,返回true * @param path 路徑 * @return 如果創建失敗(Exception) 返回false,否則true */ public static boolean createEmptyFile(String path){ File file = new File(path); if(!file.exists()){ try{ return file.createNewFile(); } catch (Exception e) { return false; } } return true; } /** * 獲取文件大小 * @param filePathName * @return */ public static long getSize(String filePathName) { if(isNullString(filePathName)) return 0; File file = new File(filePathName); if (file.isFile()) return file.length(); return 0; } /** * 讀取文件數據到byte數組 * @param filePathName 文件名 * @param readOffset 從哪裡開始讀 * @param readLength 讀取長度 * @param dataBuf 保存數據的緩沖區 * @param bufOffset 從哪裡保存 * @return */ public static boolean readData(String filePathName, int readOffset, int readLength, byte[] dataBuf, int bufOffset) { try { int readedTotalSize = 0; int onceReadSize = 0; BufferedInputStream in = new BufferedInputStream(new FileInputStream(filePathName)); in.skip(readOffset); while (readedTotalSize < readLength && (onceReadSize = in.read(dataBuf, bufOffset + readedTotalSize, readLength - readedTotalSize)) >= 0) { readedTotalSize += onceReadSize; } in.read(dataBuf, bufOffset, readLength); in.close(); in = null; } catch (Exception e) { return false; } return true; } /** 將某個流的內容輸出到文件 * @param in 輸入流 * @param filePathName 目標文件 * @return */ public static boolean writeFile(InputStream in,String filePathName){ boolean flag = false; OutputStream outStream = null; try { File destFile = new File(filePathName); if (destFile.exists()) { destFile.delete(); } else { destFile.createNewFile(); } outStream = new FileOutputStream(filePathName); byte[] buffer = new byte[1024]; int count = 0; while(true){ int length = in.read(buffer, 0, 1024); if(length > 0){ outStream.write(buffer, 0, length); }else{ break; } count+=length; } if(count > 0){ flag = true; } } catch (IOException e) { e.printStackTrace(); }finally{ if(outStream != null){ try{outStream.close();}catch(Exception e){e.printStackTrace();} } } return flag; } /** * 判斷當前字符串是否為空 * @param str * @return */ public static boolean isNullString(String str) { if(str == null || str.equals("")) return true ; return false; } /** * 復制文件 * @param fromPathName * @param toPathName * @return */ public static int copy(String fromPathName, String toPathName) { try { InputStream from = new FileInputStream(fromPathName); return copy(from, toPathName); } catch (FileNotFoundException e) { return -1; } } /** * 復制文件 * @param from * @param toPathName * @return */ public static int copy(InputStream from, String toPathName) { try { FILE.delete(toPathName); //liuwp 20120925 先刪除 OutputStream to = new FileOutputStream(toPathName); byte buf[] = new byte[1024]; int c; while ((c = from.read(buf)) > 0) { to.write(buf, 0, c); } from.close(); to.close(); return 0; } catch (Exception ex) { return -1; } } /** * 根據zip文件路徑轉換為文件路徑 * @param zipFullPath 必須帶.zip * @return */ public static String zip2FileFullPath(String zipFullPath) { int zipIndex = zipFullPath.lastIndexOf(".zip"); int zipIndexTmp = zipFullPath.lastIndexOf(".ZIP"); String tmp = ""; if(zipIndex > -1) { tmp = zipFullPath.substring(0, zipIndex); } else if(zipIndexTmp > -1) { tmp = zipFullPath.substring(0, zipIndexTmp); } return tmp; } /** * 改變文件權限 * @param permission * @param filePathName */ public static void chmod(String permission, String filePathName) { try { String command = "chmod " + permission + " " + filePathName; Runtime runtime = Runtime.getRuntime(); runtime.exec(command); } catch (IOException e) { e.printStackTrace(); } } }
在項目開發中,可能系統自帶的一些widget不能滿足我們的需求,這時就需要自定義View。通過查看系統中的常用widget如Button,TextView,EditTex
Activities提供了一種方便管理的創建、保存、回復的對話框機制,例如 onCreateDialog(int), onPrepareDialog(int, Dialo
1、gradle導入jar包的特點:(和libs文件夾導入jar包的區別)gradle導入jar包更方便,一行代碼即可搞定。不像後者那樣還要自己去官方下載。如果官方將ja
前面有文章介紹了使用GridView實現表格的方法,本文就來說說如何用ListView實現自適應的表格。GridView比ListView更容易實現自適應的表格,但是Gr