//首先判断是否有外部存储卡,如没有判断是否有内部存储卡,如没有,继续读取应用程序所在存储 if(getExternalSdCardPath() != null){ phonePicsPath = getExternalSdCardPath(); }else{ phonePicsPath = getFilesDir().getAbsolutePath(); }
/** * 遍历 "system/etc/vold.fstab” 文件,获取全部的Android的挂载点信息 * * @return */ private static ArrayListgetDevMountList() { String[] toSearch = FileUtils.readFile("/system/etc/vold.fstab").split(" "); ArrayList out = new ArrayList (); for (int i = 0; i < toSearch.length; i++) { if (toSearch[i].contains("dev_mount")) { if (new File(toSearch[i + 2]).exists()) { out.add(toSearch[i + 2]); } } } return out; }/** * 获取扩展SD卡存储目录 * * 如果有外接的SD卡,并且已挂载,则返回这个外置SD卡目录 * 否则:返回内置SD卡目录 * * @return */ public static String getExternalSdCardPath() { if (SDCardUtils.isMounted()) { File sdCardFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()); return sdCardFile.getAbsolutePath(); } String path = null; File sdCardFile = null; ArrayList devMountList = getDevMountList(); for (String devMount : devMountList) { File file = new File(devMount); if (file.isDirectory() && file.canWrite()) { path = file.getAbsolutePath(); String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date()); File testWritable = new File(path, "test_" + timeStamp); if (testWritable.mkdirs()) { testWritable.delete(); } else { path = null; } } } if (path != null) { sdCardFile = new File(path); return sdCardFile.getAbsolutePath(); } return null; }
FileUtils文件:
package com.zqsy.order.tools;/*** * 文件工具类 * @author zhangda * */import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.util.ArrayList;import java.util.List;import android.text.TextUtils;/** * File Utils *
- * Read or write file *
- {@link #readFile(String)} read file *
- {@link #readFileToList(String)} read file to string list *
- {@link #writeFile(String, String, boolean)} write file from String *
- {@link #writeFile(String, String)} write file from String *
- {@link #writeFile(String, List, boolean)} write file from String List *
- {@link #writeFile(String, List)} write file from String List *
- {@link #writeFile(String, InputStream)} write file *
- {@link #writeFile(String, InputStream, boolean)} write file *
- {@link #writeFile(File, InputStream)} write file *
- {@link #writeFile(File, InputStream, boolean)} write file *
- * Operate file *
- {@link #moveFile(File, File)} or {@link #moveFile(String, String)} *
- {@link #copyFile(String, String)} *
- {@link #getFileExtension(String)} *
- {@link #getFileName(String)} *
- {@link #getFileNameWithoutExtension(String)} *
- {@link #getFileSize(String)} *
- {@link #deleteFile(String)} *
- {@link #isFileExist(String)} *
- {@link #isFolderExist(String)} *
- {@link #makeFolders(String)} *
- {@link #makeDirs(String)} *
} * @return if file not exist, return null, else return content of file * @throws RuntimeException if an error occurs while operator BufferedReader */ public static String readFile(String filePath) { String fileContent = ""; File file = new File(filePath); if (file == null || !file.isFile()) { return null; } BufferedReader reader = null; try { InputStreamReader is = new InputStreamReader(new FileInputStream(file)); reader = new BufferedReader(is); String line = null; int i = 0; while ((line = reader.readLine()) != null) { fileContent += line + " "; } reader.close(); return fileContent; } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return fileContent; } /** * write file * * @param filePath * @param content * @param append is append, if true, write to the end of file, else clear content of file and write into it * @return return false if content is empty, true otherwise * @throws RuntimeException if an error occurs while operator FileWriter */ public static boolean writeFile(String filePath, String content, boolean append) { if (StringUtils.isEmpty(content)) { return false; } FileWriter fileWriter = null; try { makeDirs(filePath); fileWriter = new FileWriter(filePath, append); fileWriter.write(content); fileWriter.close(); return true; } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } finally { if (fileWriter != null) { try { fileWriter.close(); } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } } } } /** * write file * * @param filePath * @param contentList * @param append is append, if true, write to the end of file, else clear content of file and write into it * @return return false if contentList is empty, true otherwise * @throws RuntimeException if an error occurs while operator FileWriter */ public static boolean writeFile(String filePath, List contentList, boolean append) { if (contentList == null || contentList.size() == 0) { return false; } FileWriter fileWriter = null; try { makeDirs(filePath); fileWriter = new FileWriter(filePath, append); int i = 0; for (String line : contentList) { if (i++ > 0) { fileWriter.write("\r\n"); } fileWriter.write(line); } fileWriter.close(); return true; } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } finally { if (fileWriter != null) { try { fileWriter.close(); } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } } } } /** * write file, the string will be written to the begin of the file * * @param filePath * @param content * @return */ public static boolean writeFile(String filePath, String content) { return writeFile(filePath, content, false); } /** * write file, the string list will be written to the begin of the file * * @param filePath * @param contentList * @return */ public static boolean writeFile(String filePath, List contentList) { return writeFile(filePath, contentList, false); } /** * write file, the bytes will be written to the begin of the file * * @param filePath * @param stream * @return * @see {@link #writeFile(String, InputStream, boolean)} */ public static boolean writeFile(String filePath, InputStream stream) { return writeFile(filePath, stream, false); } /** * write file * * @param file the file to be opened for writing. * @param stream the input stream * @param append if true
, then bytes will be written to the end of the file rather than the beginning * @return return true * @throws RuntimeException if an error occurs while operator FileOutputStream */ public static boolean writeFile(String filePath, InputStream stream, boolean append) { return writeFile(filePath != null ? new File(filePath) : null, stream, append); } /** * write file, the bytes will be written to the begin of the file * * @param file * @param stream * @return * @see {@link #writeFile(File, InputStream, boolean)} */ public static boolean writeFile(File file, InputStream stream) { return writeFile(file, stream, false); } /** * write file * * @param file the file to be opened for writing. * @param stream the input stream * @param append if true
, then bytes will be written to the end of the file rather than the beginning * @return return true * @throws RuntimeException if an error occurs while operator FileOutputStream */ public static boolean writeFile(File file, InputStream stream, boolean append) { OutputStream o = null; try { makeDirs(file.getAbsolutePath()); o = new FileOutputStream(file, append); byte data[] = new byte[1024]; int length = -1; while ((length = stream.read(data)) != -1) { o.write(data, 0, length); } o.flush(); return true; } catch (FileNotFoundException e) { throw new RuntimeException("FileNotFoundException occurred. ", e); } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } finally { if (o != null) { try { o.close(); stream.close(); } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } } } } /** * move file * * @param sourceFilePath * @param destFilePath */ public static void moveFile(String sourceFilePath, String destFilePath) { if (TextUtils.isEmpty(sourceFilePath) || TextUtils.isEmpty(destFilePath)) { throw new RuntimeException("Both sourceFilePath and destFilePath cannot be null."); } moveFile(new File(sourceFilePath), new File(destFilePath)); } /** * move file * * @param srcFile * @param destFile */ public static void moveFile(File srcFile, File destFile) { boolean rename = srcFile.renameTo(destFile); if (!rename) { copyFile(srcFile.getAbsolutePath(), destFile.getAbsolutePath()); deleteFile(srcFile.getAbsolutePath()); } } /** * copy file * * @param sourceFilePath * @param destFilePath * @return * @throws RuntimeException if an error occurs while operator FileOutputStream */ public static boolean copyFile(String sourceFilePath, String destFilePath) { InputStream inputStream = null; try { inputStream = new FileInputStream(sourceFilePath); } catch (FileNotFoundException e) { throw new RuntimeException("FileNotFoundException occurred. ", e); } return writeFile(destFilePath, inputStream); } /** * read file to string list, a element of list is a line * * @param filePath * @param charsetName The name of a supported {@link java.nio.charset.Charset
charset} * @return if file not exist, return null, else return content of file * @throws RuntimeException if an error occurs while operator BufferedReader */ public static List readFileToList(String filePath, String charsetName) { File file = new File(filePath); List fileContent = new ArrayList (); if (file == null || !file.isFile()) { return null; } BufferedReader reader = null; try { InputStreamReader is = new InputStreamReader(new FileInputStream(file), charsetName); reader = new BufferedReader(is); String line = null; while ((line = reader.readLine()) != null) { fileContent.add(line); } reader.close(); return fileContent; } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { throw new RuntimeException("IOException occurred. ", e); } } } } /** * get file name from path, not include suffix * * * getFileNameWithoutExtension(null) = null * getFileNameWithoutExtension("") = "" * getFileNameWithoutExtension(" ") = " " * getFileNameWithoutExtension("abc") = "abc" * getFileNameWithoutExtension("a.mp3") = "a" * getFileNameWithoutExtension("a.b.rmvb") = "a.b" * getFileNameWithoutExtension("c:\\") = "" * getFileNameWithoutExtension("c:\\a") = "a" * getFileNameWithoutExtension("c:\\a.b") = "a" * getFileNameWithoutExtension("c:a.txt\\a") = "a" * getFileNameWithoutExtension("/home/admin") = "admin" * getFileNameWithoutExtension("/home/admin/a.txt/b.mp3") = "b" *
* * @param filePath * @return file name from path, not include suffix * @see */ public static String getFileNameWithoutExtension(String filePath) { if (StringUtils.isEmpty(filePath)) { return filePath; } int extenPosi = filePath.lastIndexOf(FILE_EXTENSION_SEPARATOR); int filePosi = filePath.lastIndexOf(File.separator); if (filePosi == -1) { return (extenPosi == -1 ? filePath : filePath.substring(0, extenPosi)); } if (extenPosi == -1) { return filePath.substring(filePosi + 1); } return (filePosi < extenPosi ? filePath.substring(filePosi + 1, extenPosi) : filePath.substring(filePosi + 1)); } /** * get file name from path, include suffix * * * getFileName(null) = null * getFileName("") = "" * getFileName(" ") = " " * getFileName("a.mp3") = "a.mp3" * getFileName("a.b.rmvb") = "a.b.rmvb" * getFileName("abc") = "abc" * getFileName("c:\\") = "" * getFileName("c:\\a") = "a" * getFileName("c:\\a.b") = "a.b" * getFileName("c:a.txt\\a") = "a" * getFileName("/home/admin") = "admin" * getFileName("/home/admin/a.txt/b.mp3") = "b.mp3" *
* * @param filePath * @return file name from path, include suffix */ public static String getFileName(String filePath) { if (StringUtils.isEmpty(filePath)) { return filePath; } int filePosi = filePath.lastIndexOf(File.separator); return (filePosi == -1) ? filePath : filePath.substring(filePosi + 1); } /** * get folder name from path * * * getFolderName(null) = null * getFolderName("") = "" * getFolderName(" ") = "" * getFolderName("a.mp3") = "" * getFolderName("a.b.rmvb") = "" * getFolderName("abc") = "" * getFolderName("c:\\") = "c:" * getFolderName("c:\\a") = "c:" * getFolderName("c:\\a.b") = "c:" * getFolderName("c:a.txt\\a") = "c:a.txt" * getFolderName("c:a\\b\\c\\d.txt") = "c:a\\b\\c" * getFolderName("/home/admin") = "/home" * getFolderName("/home/admin/a.txt/b.mp3") = "/home/admin/a.txt" *
* * @param filePath * @return */ public static String getFolderName(String filePath) { if (StringUtils.isEmpty(filePath)) { return filePath; } int filePosi = filePath.lastIndexOf(File.separator); return (filePosi == -1) ? "" : filePath.substring(0, filePosi); } /** * get suffix of file from path * * * getFileExtension(null) = "" * getFileExtension("") = "" * getFileExtension(" ") = " " * getFileExtension("a.mp3") = "mp3" * getFileExtension("a.b.rmvb") = "rmvb" * getFileExtension("abc") = "" * getFileExtension("c:\\") = "" * getFileExtension("c:\\a") = "" * getFileExtension("c:\\a.b") = "b" * getFileExtension("c:a.txt\\a") = "" * getFileExtension("/home/admin") = "" * getFileExtension("/home/admin/a.txt/b") = "" * getFileExtension("/home/admin/a.txt/b.mp3") = "mp3" *
* * @param filePath * @return */ public static String getFileExtension(String filePath) { if (StringUtils.isBlank(filePath)) { return filePath; } int extenPosi = filePath.lastIndexOf(FILE_EXTENSION_SEPARATOR); int filePosi = filePath.lastIndexOf(File.separator); if (extenPosi == -1) { return ""; } return (filePosi >= extenPosi) ? "" : filePath.substring(extenPosi + 1); } /** * Creates the directory named by the trailing filename of this file, including the complete directory path required * to create this directory. * * * Attentions: * - makeDirs("C:\\Users\\Trinea") can only create users folder
* - makeFolder("C:\\Users\\Trinea\\") can create Trinea folder
*
* * @param filePath * @return true if the necessary directories have been created or the target directory already exists, false one of * the directories can not be created. * * - if {@link FileUtils#getFolderName(String)} return null, return false
* - if target directory already exists, return true
* - return {@link java.io.File#makeFolder}
*
*/ public static boolean makeDirs(String filePath) { String folderName = getFolderName(filePath); if (StringUtils.isEmpty(folderName)) { return false; } File folder = new File(folderName); return (folder.exists() && folder.isDirectory()) ? true : folder.mkdirs(); } /** * @param filePath * @return * @see #makeDirs(String) */ public static boolean makeFolders(String filePath) { return makeDirs(filePath); } /** * Indicates if this file represents a file on the underlying file system. * * @param filePath * @return */ public static boolean isFileExist(String filePath) { if (StringUtils.isBlank(filePath)) { return false; } File file = new File(filePath); return (file.exists() && file.isFile()); } /** * Indicates if this file represents a directory on the underlying file system. * * @param directoryPath * @return */ public static boolean isFolderExist(String directoryPath) { if (StringUtils.isBlank(directoryPath)) { return false; } File dire = new File(directoryPath); return (dire.exists() && dire.isDirectory()); } /** * delete file or directory * * - if path is null or empty, return true
* - if path not exist, return true
* - if path exist, delete recursion. return true
* * * @param path * @return */ public static boolean deleteFile(String path) { if (StringUtils.isBlank(path)) { return true; } File file = new File(path); if (!file.exists()) { return true; } if (file.isFile()) { return file.delete(); } if (!file.isDirectory()) { return false; } for (File f : file.listFiles()) { if (f.isFile()) { f.delete(); } else if (f.isDirectory()) { deleteFile(f.getAbsolutePath()); } } return file.delete(); } /** * get file size * * - if path is null or empty, return -1
* - if path exist and it is a file, return file size, else return -1
* * * @param path * @return returns the length of this file in bytes. returns -1 if the file does not exist. */ public static long getFileSize(String path) { if (StringUtils.isBlank(path)) { return -1; } File file = new File(path); return (file.exists() && file.isFile() ? file.length() : -1); }}
StringUtils文件
package com.zqsy.order.tools;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * String Utils * * @author Trinea 2011-7-22 */public class StringUtils { private StringUtils() { throw new AssertionError(); } /** * is null or its length is 0 or it is made by space * ** isBlank(null) = true; * isBlank("") = true; * isBlank(" ") = true; * isBlank("a") = false; * isBlank("a ") = false; * isBlank(" a") = false; * isBlank("a b") = false; ** * @param str * @return if string is null or its size is 0 or it is made by space, return true, else return false. */ public static boolean isBlank(String str) { return (str == null || str.trim().length() == 0); } /** * is null or its length is 0 * ** isEmpty(null) = true; * isEmpty("") = true; * isEmpty(" ") = false; ** * @param str * @return if string is null or its size is 0, return true, else return false. */ public static boolean isEmpty(CharSequence str) { return (str == null || str.length() == 0); } /** * compare two string * * @param actual * @param expected * @return * @see ObjectUtils#isEquals(Object, Object) */ public static boolean isEquals(String actual, String expected) { return actual == expected || (actual == null ? expected == null : actual.equals(expected)); } /** * get length of CharSequence * ** length(null) = 0; * length(\"\") = 0; * length(\"abc\") = 3; ** * @param str * @return if str is null or empty, return 0, else return {@link CharSequence#length()}. */ public static int length(CharSequence str) { return str == null ? 0 : str.length(); } /** * null Object to empty string * ** nullStrToEmpty(null) = ""; * nullStrToEmpty("") = ""; * nullStrToEmpty("aa") = "aa"; ** * @param str * @return */ public static String nullStrToEmpty(Object str) { return (str == null ? "" : (str instanceof String ? (String)str : str.toString())); } /** * capitalize first letter * ** capitalizeFirstLetter(null) = null; * capitalizeFirstLetter("") = ""; * capitalizeFirstLetter("2ab") = "2ab" * capitalizeFirstLetter("a") = "A" * capitalizeFirstLetter("ab") = "Ab" * capitalizeFirstLetter("Abc") = "Abc" ** * @param str * @return */ public static String capitalizeFirstLetter(String str) { if (isEmpty(str)) { return str; } char c = str.charAt(0); return (!Character.isLetter(c) || Character.isUpperCase(c)) ? str : new StringBuilder(str.length()) .append(Character.toUpperCase(c)).append(str.substring(1)).toString(); } /** * encoded in utf-8 * ** utf8Encode(null) = null * utf8Encode("") = ""; * utf8Encode("aa") = "aa"; * utf8Encode("啊啊啊啊") = "%E5%95%8A%E5%95%8A%E5%95%8A%E5%95%8A"; ** * @param str * @return * @throws UnsupportedEncodingException if an error occurs */ public static String utf8Encode(String str) { if (!isEmpty(str) && str.getBytes().length != str.length()) { try { return URLEncoder.encode(str, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException("UnsupportedEncodingException occurred. ", e); } } return str; } /** * encoded in utf-8, if exception, return defultReturn * * @param str * @param defultReturn * @return */ public static String utf8Encode(String str, String defultReturn) { if (!isEmpty(str) && str.getBytes().length != str.length()) { try { return URLEncoder.encode(str, "UTF-8"); } catch (UnsupportedEncodingException e) { return defultReturn; } } return str; } /** * get innerHtml from href * ** getHrefInnerHtml(null) = "" * getHrefInnerHtml("") = "" * getHrefInnerHtml("mp3") = "mp3"; * getHrefInnerHtml("<a innerHtml</a>") = "<a innerHtml</a>"; * getHrefInnerHtml("<a>innerHtml</a>") = "innerHtml"; * getHrefInnerHtml("<a<a>innerHtml</a>") = "innerHtml"; * getHrefInnerHtml("<a href="baidu.com">innerHtml</a>") = "innerHtml"; * getHrefInnerHtml("<a href="baidu.com" title="baidu">innerHtml</a>") = "innerHtml"; * getHrefInnerHtml(" <a>innerHtml</a> ") = "innerHtml"; * getHrefInnerHtml("<a>innerHtml</a></a>") = "innerHtml"; * getHrefInnerHtml("jack<a>innerHtml</a></a>") = "innerHtml"; * getHrefInnerHtml("<a>innerHtml1</a><a>innerHtml2</a>") = "innerHtml2"; ** * @param href * @return
- *
- if href is null, return "" *
- if not match regx, return source *
- return the last string that match regx *
* htmlEscapeCharsToString(null) = null; * htmlEscapeCharsToString("") = ""; * htmlEscapeCharsToString("mp3") = "mp3"; * htmlEscapeCharsToString("mp3<") = "mp3<"; * htmlEscapeCharsToString("mp3>") = "mp3\>"; * htmlEscapeCharsToString("mp3&mp4") = "mp3&mp4"; * htmlEscapeCharsToString("mp3"mp4") = "mp3\"mp4"; * htmlEscapeCharsToString("mp3<>&"mp4") = "mp3\<\>&\"mp4"; ** * @param source * @return */ public static String htmlEscapeCharsToString(String source) { return StringUtils.isEmpty(source) ? source : source.replaceAll("<", "<").replaceAll(">", ">") .replaceAll("&", "&").replaceAll(""", "\""); } /** * transform half width char to full width char * *
* fullWidthToHalfWidth(null) = null; * fullWidthToHalfWidth("") = ""; * fullWidthToHalfWidth(new String(new char[] {12288})) = " "; * fullWidthToHalfWidth("!"#$%&) = "!\"#$%&"; ** * @param s * @return */ public static String fullWidthToHalfWidth(String s) { if (isEmpty(s)) { return s; } char[] source = s.toCharArray(); for (int i = 0; i < source.length; i++) { if (source[i] == 12288) { source[i] = ' '; // } else if (source[i] == 12290) { // source[i] = '.'; } else if (source[i] >= 65281 && source[i] <= 65374) { source[i] = (char)(source[i] - 65248); } else { source[i] = source[i]; } } return new String(source); } /** * transform full width char to half width char * *
* halfWidthToFullWidth(null) = null; * halfWidthToFullWidth("") = ""; * halfWidthToFullWidth(" ") = new String(new char[] {12288}); * halfWidthToFullWidth("!\"#$%&) = "!"#$%&"; ** * @param s * @return */ public static String halfWidthToFullWidth(String s) { if (isEmpty(s)) { return s; } char[] source = s.toCharArray(); for (int i = 0; i < source.length; i++) { if (source[i] == ' ') { source[i] = (char)12288; // } else if (source[i] == '.') { // source[i] = (char)12290; } else if (source[i] >= 33 && source[i] <= 126) { source[i] = (char)(source[i] + 65248); } else { source[i] = source[i]; } } return new String(source); }}