Java tutorial
/* * Appspresso * * Copyright (c) 2011 KT Hitel Corp. * * Appspresso SDK may be freely distributed under the MIT license. */ package com.appspresso.api.fs; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import org.apache.commons.logging.Log; import android.text.TextUtils; import com.appspresso.api.AxLog; /** * *** INTERNAL USE ONLY *** * @version 1.0 */ public class FileSystemUtils { public static final Log L = AxLog.getLog(AxFile.class.getSimpleName()); public static final String EMPTY_PATH = ""; public static final int BUFFER_SIZE = 4096; /** * path component ? ? ? path . * * @param paths path component * @return ? path */ public static String mergePath(String... paths) { String result = EMPTY_PATH; for (String path : paths) { if (0 == path.length()) continue; if (0 != result.length()) result += File.separator; result += path; } return result; } /** * path? ? path separator . * * @param path separator path * @return path separator ? path */ public static String removeExtraFileSeparator(String path) { int index = path.indexOf(File.separator); if (0 == index) { path = path.substring(1); } index = path.lastIndexOf(File.separator); if (path.length() - 1 == index) { path = path.substring(0, index); } return path; } /** * path path separator . * * @param path path * @return ? path component? */ public static String[] split(String path) { String[] pathArray = path.split(File.separator); ArrayList<String> newPathArray = new ArrayList<String>(); int length = pathArray.length; for (int i = 0; i < length; i++) { if (TextUtils.isEmpty(pathArray[i])) continue; newPathArray.add(pathArray[i]); } return newPathArray.toArray(new String[newPathArray.size()]); } /** * path prefix ?. * * @param path ? path * @param prefix path ? prefix * @return path prefix {@literal true}, {@literal false} . */ public static boolean hasPrefix(String path, String prefix) { if (!path.startsWith(prefix)) return false; if (path.equals(prefix)) return true; return File.separatorChar == path.charAt(prefix.length()); } /** * path? prefix path? . * * @param path ? path * @param prefix prefix * @return prefix ? path */ public static String extractRelativePath(String path, String prefix) { try { return path.substring(prefix.length() + 1); } catch (StringIndexOutOfBoundsException e) { if (path.equals(prefix)) return EMPTY_PATH; return null; } } /** * path? name? . name? ?? ? path? path component ?. * * @param path name? path * @return ? path */ public static String extractName(String path) { int index = path.lastIndexOf(File.separator); return path.substring(index + 1); } /** * path? prefix prefix . * * @param path path * @param oldPrefix ? prefix * @param newPrefix prefix * @return prefix ? path */ public static String replacePrefix(String path, String oldPrefix, String newPrefix) { String relativePath = extractRelativePath(path, oldPrefix); return mergePath(newPrefix, relativePath); } /** * path? name name? path . * * @param path path * @return ? name? path, ? path? name? ? 2? */ public static String[] divideToParentPathAndName(String path) { path = removeExtraFileSeparator(path); int index = path.lastIndexOf(File.separator); if (-1 == index) { return new String[] { EMPTY_PATH, path }; } else { return new String[] { path.substring(0, index), path.substring(index + 1) }; } } /** * InputStream path? ? . * * @param inputStream ? InputStream * @param destFilePath ?? path * @param overwrite path? ? ?? ? * @return ? {@literal true}, {@literal false} * @throws IOException ?? ?. */ public static boolean copy(InputStream inputStream, String destFilePath, boolean overwrite) throws IOException { File destFile = new File(destFilePath); File parent = destFile.getParentFile(); if (!parent.exists() && !parent.mkdirs() && !parent.mkdir()) { return false; } if (destFile.exists()) { if (!overwrite) { return false; } if (!destFile.delete()) { return false; } } if (!destFile.createNewFile()) { return false; } FileOutputStream outputStream = null; try { outputStream = new FileOutputStream(destFile); BufferedOutputStream bOutputStream = new BufferedOutputStream(outputStream); byte[] buffer = new byte[BUFFER_SIZE]; int length = -1; while ((length = inputStream.read(buffer, 0, BUFFER_SIZE)) != -1) { bOutputStream.write(buffer, 0, length); } bOutputStream.flush(); return true; } finally { closeQuietly(inputStream); closeQuietly(outputStream); } } /** * InputStream? ?. * * @param in ? InputStream */ public static void closeQuietly(InputStream in) { if (null != in) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * OutputStream? ?. * * @param out ? OutputStream */ public static void closeQuietly(OutputStream out) { if (null != out) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * InputStream ? ? ? . * * @param inputStream ? InputStream * @param encoding ? * @return ? ? * @throws UnsupportedEncodingException ? ? * @throws OutOfMemoryError * @throws IOException ? ? ? */ public static String readAsText(InputStream inputStream, String encoding) throws UnsupportedEncodingException, OutOfMemoryError, IOException { try { InputStreamReader inputStreamReader = new InputStreamReader(inputStream, encoding); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuffer buffer = new StringBuffer(); String temp = null; while ((temp = bufferedReader.readLine()) != null) { buffer.append(temp); } return buffer.toString(); } finally { closeQuietly(inputStream); } } /** * path null? ?? * * @param path path * @return path null? ?? {@literal true}, {@literal false} */ public static boolean isEmptyPath(String path) { return EMPTY_PATH.equals(path); } }