开发学院手机开发Android 开发 Android 处理txt文件类FileUtils 利用java反射机制... 阅读

Android 处理txt文件类FileUtils 利用java反射机制访问非sdk类和函数

 2010-09-10 00:59:00 来源:WEB开发网   
核心提示:try {if (destFile.exists()) {destFile.delete();}OutputStream out = new FileOutputStream(destFile);try {byte[] buffer = new byte[4096];int bytesRead;while ((byte

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;

上一页  1 2 3 4 5  下一页

Tags:Android 处理 txt

编辑录入:coldstar [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接