android 反射机制

来源:技术人生 责任编辑:栏目编辑 发表时间:2013-07-01 23:08 点击:
Android系统内部提供了一个不错的txt文本读写类,但目前并没有公开提供给标准的SDK,FileUtils类的源代码如下,可以很好的操作Linux下的文本文件。
  public class FileUtils
  {
  public static final int S_IRWXU = 00700;
  public static final int S_IRUSR = 00400;
  public static final int S_IWUSR = 00200;
  public static final int S_IXUSR = 00100;
  public static final int S_IRWXG = 00070;
  public static final int S_IRGRP = 00040;
  public static final int S_IWGRP = 00020;
  public static final int S_IXGRP = 00010;
  public static final int S_IRWXO = 00007;
  public static final int S_IROTH = 00004;
  public static final int S_IWOTH = 00002;
  public static final int S_IXOTH = 00001;
  public static final class FileStatus {
  public int dev;
  public int ino;
  public int mode;
  public int nlink;
  public int uid;
  public int gid;
  public int rdev;
  public long size;
  public int blksize;
  public long blocks;
  public long atime;
  public long mtime;
  public long ctime;
  }
  public static native boolean getFileStatus (String path, FileStatus status);
  private static final Pattern SAFE_FILENAME_PATTERN = Pattern.compile("[\w%+,./=_-]+");
  public static boolean copyFile(File srcFile, File destFile) {
  boolean result = false;
  try {
  InputStream in = new FileInputStream(srcFile);
  try {
  result = copyToFile(in, destFile);
  } finally {
  in.close();
  }
  } catch (IOException e) {
  result = false;
  }
  return result;
  }
  public static boolean copyToFile (InputStream inputStream, File destFile) {
======黑软基地手机资讯频道======
  
  try {
  if (destFile.exists()) {
  destFile.delete();
  }
  OutputStream out = new FileOutputStream(destFile);
  try {
  byte[] buffer = new byte[4096];
  int bytesRead;
  while ((bytesRead = inputStream.read(buffer)) >= 0) {
  out.write(buffer, 0, bytesRead);
  }
  } finally {
  out.close();
  }
  return true;
  } catch (IOException e) {
  return false;
  }
  }
  public static boolean isFilenameSafe(File file) {
  return SAFE_FILENAME_PATTERN.matcher(file.getPath()).matches();
  }
  public static String readTextFile(File file, int max, String ellipsis) throws IOException {
  InputStream input = new FileInputStream(file);
  try {
  long size = file.length();
  if (max > 0 || (size > 0 && max == 0)) {
  if (size > 0 && (max == 0 || size < max)) max = (int) size;
  byte[] data = new byte[max + 1];
  int length = input.read(data);
  if (length <= 0) return "";
  if (length <= max) return new String(data, 0, length);
  if (ellipsis == null) return new String(data, 0, max);
  return new String(data, 0, max) + ellipsis;
  } else if (max < 0) { // "tail" mode: keep the last N
  int len;
  boolean rolled = false;
  byte[] last = null, data = null;
  do {
  if (last != null) rolled = true;
  byte[] tmp = last; last = data; data = tmp;
  if (data == null) data = new byte[-max];
  len = input.read(data);
  } while (len == data.length);
  if (last == null && len <= 0) return "";
  if (last == null) return new String(data, 0, len);
  if (len > 0) {
  rolled = true;
======黑软基地手机资讯频道======
  
  System.arraycopy(last, len, last, 0, last.length - len);
  System.arraycopy(data, 0, last, last.length - len, len);
  }
  if (ellipsis == null || !rolled) return new String(last);
  return ellipsis + new String(last);
  }

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • Android 完全退出程序
      • 原创:Android应用开发-Andorid歌词秀,含源码
      • android 屏幕保护
      • Android手机软件汉化教程---第四课 dex文件汉化
      • 众多Android 开源项目推荐,给力工作给力学习
      • Android Audio代码分析4
      • Android得到已安装的应用程序信息!
      • Android开发者指南(29) —— USB Host and Accessory
      • Android成长的幕后推手:工程师鲁宾
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1