一、前言
关于informa源码包中的de.nava.informa.utils.FileUtils文件工具类,实现基于字符串String/本地文件File/缓存BufferedReader数据几种不同方式对比compare、本地文件复制拷贝copyFile操作,具体如下源码说明部分。
二、源码说明
package de.nava.informa.utils;@b@@b@import java.io.BufferedReader;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileOutputStream;@b@import java.io.FileReader;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.OutputStream;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@@b@public final class FileUtils@b@{@b@ private static Log logger = LogFactory.getLog(FileUtils.class);@b@@b@ public static boolean compare(String paramString1, String paramString2)@b@ throws IOException@b@ {@b@ return compare(new File(paramString1), new File(paramString2));@b@ }@b@@b@ public static boolean compare(File paramFile1, File paramFile2)@b@ throws IOException@b@ {@b@ BufferedReader localBufferedReader1;@b@ try@b@ {@b@ logger.debug("Comparing golden file " + paramFile1 + " to " + paramFile2);@b@ localBufferedReader1 = new BufferedReader(new FileReader(paramFile1));@b@ }@b@ catch (IOException localIOException)@b@ {@b@ logger.error("Could not read baseline: " + localIOException);@b@ return false;@b@ }@b@ BufferedReader localBufferedReader2 = new BufferedReader(new FileReader(paramFile2));@b@ return compare(localBufferedReader1, localBufferedReader2);@b@ }@b@@b@ private static boolean compare(BufferedReader paramBufferedReader1, BufferedReader paramBufferedReader2)@b@ throws IOException@b@ {@b@ String str1 = paramBufferedReader1.readLine();@b@ for (String str2 = paramBufferedReader2.readLine(); (str1 != null) && (str2 != null); str2 = paramBufferedReader2.readLine())@b@ {@b@ if ((str1 == null) || (str2 == null))@b@ return false;@b@ if (!(str1.equals(str2)))@b@ return false;@b@ str1 = paramBufferedReader1.readLine();@b@ }@b@ paramBufferedReader1.close();@b@ paramBufferedReader2.close();@b@ return ((str1 == null) && (str2 == null));@b@ }@b@@b@ public static void copyFile(File paramFile1, File paramFile2)@b@ {@b@ try@b@ {@b@ logger.debug("Copying file " + paramFile1 + " to " + paramFile2);@b@ FileInputStream localFileInputStream = new FileInputStream(paramFile1);@b@ FileOutputStream localFileOutputStream = new FileOutputStream(paramFile2);@b@ byte[] arrayOfByte = new byte[8192];@b@ while ((i = localFileInputStream.read(arrayOfByte)) >= 0)@b@ {@b@ int i;@b@ localFileOutputStream.write(arrayOfByte, 0, i);@b@ localFileOutputStream.flush();@b@ }@b@ localFileInputStream.close();@b@ localFileOutputStream.close();@b@ }@b@ catch (Exception localException)@b@ {@b@ logger.warn("Error occurred while copying file " + paramFile1 + " to " + paramFile2);@b@ localException.printStackTrace();@b@ }@b@ }@b@}