Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 輔助工具類——文件簡單操作輔助類

android 輔助工具類——文件簡單操作輔助類

編輯:關於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(); } } }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved