FileUtil(from logparser)

时间:2020-11-28 15:10:50
 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map; import org.apache.log4j.Logger; public class FileUtil
{
static Logger logger; static {
FileUtil.logger = Logger.getLogger((Class)FileUtil.class);
}
public static <T> T readFile(final String filePath, T container, final String encode) {
try {
final File file = new File(filePath);
if (!file.exists()) {
FileUtil.logger.info((Object)("file " + filePath + " does not exists."));
return container;
}
final FileInputStream fis = new FileInputStream(file);
final InputStreamReader isr = new InputStreamReader(fis, (encode == null) ? "GBK" : encode);
final BufferedReader br = new BufferedReader(isr);
if (container instanceof List) {
final List list = (List)container;
String string = null;
while ((string = br.readLine()) != null) {
list.add(string);
}
}
else if (container instanceof String[]) {
String string2 = null;
final List<String> list2 = new ArrayList<String>();
while ((string2 = br.readLine()) != null) {
list2.add(string2);
}
final int count = list2.size();
final String[] strings = new String[count];
for (int i = 0; i < count; ++i) {
strings[i] = list2.get(i);
}
container = (T)(Object)strings;
}
else if (container instanceof Map) {
String string2 = null;
final Map map = (Map)container;
while ((string2 = br.readLine()) != null) {
final int a = string2.indexOf("=");
if (a != -1) {
map.put(string2.substring(0, a), string2.substring(a + 1));
}
}
}
else if (container instanceof byte[]) {
container = (T)getInputStreamBytes(fis);
}
br.close();
isr.close();
fis.close();
}
catch (Exception e1) {
FileUtil.logger.error((Object)"", (Throwable)e1);
}
return container;
} public static <T> T readFile(final InputStream is, T container, final String encode) {
try {
final InputStreamReader isr = new InputStreamReader(is, (encode == null) ? "GBK" : encode);
final BufferedReader br = new BufferedReader(isr);
if (container instanceof List) {
final List list = (List)container;
String string = null;
while ((string = br.readLine()) != null) {
list.add(string);
}
}
else if (container instanceof String[]) {
String string2 = null;
final List<String> list2 = new ArrayList<String>();
while ((string2 = br.readLine()) != null) {
list2.add(string2);
}
final int count = list2.size();
final String[] strings = new String[count];
for (int i = 0; i < count; ++i) {
strings[i] = list2.get(i);
}
container = (T)(Object)strings;
}
else if (container instanceof Map) {
String string2 = null;
final Map map = (Map)container;
while ((string2 = br.readLine()) != null) {
final int a = string2.indexOf("=");
if (a != -1) {
map.put(string2.substring(0, a), string2.substring(a + 1));
}
}
}
else if (container instanceof byte[]) {
container = (T)getInputStreamBytes(is);
}
br.close();
isr.close();
is.close();
}
catch (Exception e1) {
FileUtil.logger.error((Object)"", (Throwable)e1);
}
return container;
} public static void writeFile(final String directory, final String filename, final String encode, final Object... objs) {
try {
if (objs != null && objs.length > 0) {
final File file = new File(directory);
if (!file.exists()) {
file.mkdirs();
}
final FileOutputStream fos = new FileOutputStream(String.valueOf(directory) + "/" + filename);
final OutputStreamWriter osw = new OutputStreamWriter(fos, (encode == null) ? "GBK" : encode);
final BufferedWriter bw = new BufferedWriter(osw);
if (objs.length > 1) {
for (int len = objs.length, i = 0; i < len; ++i) {
final Object object = objs[i];
bw.write((object == null) ? "" : object.toString());
if (i != len - 1) {
bw.newLine();
}
}
}
else if (objs[0] instanceof List) {
final List list = (List)objs[0];
final Iterator iterator = list.iterator();
Object object = null;
while (true) {
if (iterator.hasNext()) {
object = iterator.next();
bw.write((object == null) ? "" : object.toString());
}
if (!iterator.hasNext()) {
break;
}
bw.newLine();
}
}
else if (objs[0] instanceof String[]) {
final String[] objects = (String[])objs[0];
for (int len2 = objects.length, j = 0; j < len2; ++j) {
final String object2 = objects[j];
bw.write((object2 == null) ? "" : object2);
if (j != len2 - 1) {
bw.newLine();
}
}
}
else if (objs[0] instanceof Map) {
final Map map = (Map)objs[0];
final int size = map.size();
int count = 0;
for (final Object key : map.keySet()) {
final Object value = map.get(key);
bw.write(String.valueOf((key == null) ? "" : key.toString()) + "=" + ((value == null) ? "" : value.toString()));
if (++count != size) {
bw.newLine();
}
}
}
else if (objs[0] instanceof byte[]) {
final byte[] bs = (byte[])objs[0];
fos.write(bs);
}
else {
bw.write(objs[0].toString());
}
bw.close();
osw.close();
fos.close();
}
}
catch (Exception e) {
FileUtil.logger.error((Object)"", (Throwable)e);
}
} public static <T> T readFileSafely(final String filePath, final T container) {
final String encode = getEncoding(filePath);
return readFile(filePath, container, encode);
} public static void repOldStr(final File file, final String oldStr, final String newStr) {
final String encode = getEncoding(file.getPath());
final List<String> list = readFile(file.getPath(), new ArrayList<String>(), encode);
final List<String> list2 = new ArrayList<String>();
for (String str : list) {
str = str.replace(oldStr, newStr);
list2.add(str);
}
writeFile(file.getParent(), file.getName(), encode, list2);
} private static byte[] getInputStreamBytes(final InputStream is) throws IOException {
final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
final byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
final byte[] data = outStream.toByteArray();
return data;
} public static boolean appendFile(final String filepath, final String... content) {
if (content == null || content.length == 0) {
return true;
}
try {
final FileWriter fw = new FileWriter(filepath, true);
for (int i = 0; i < content.length; ++i) {
final String s = content[i];
fw.write(String.valueOf(s) + "\r\n");
}
fw.close();
return true;
}
catch (Exception e) {
FileUtil.logger.error((Object)"", (Throwable)e);
return false;
}
}
public static String getEncoding(final String filepath) {
return EncodingDetect.getJavaEncode(filepath);
}
public static void main(String[] args) {
String file = "C:\\Users\\lenovo\\Downloads\\a.log";
String encode=EncodingDetect.getJavaEncode(file);
System.out.println(encode);
}
}