zoukankan      html  css  js  c++  java
  • FileUtil(from logparser)

      1 import java.io.BufferedReader;
      2 import java.io.BufferedWriter;
      3 import java.io.ByteArrayOutputStream;
      4 import java.io.File;
      5 import java.io.FileInputStream;
      6 import java.io.FileOutputStream;
      7 import java.io.FileWriter;
      8 import java.io.IOException;
      9 import java.io.InputStream;
     10 import java.io.InputStreamReader;
     11 import java.io.OutputStreamWriter;
     12 import java.util.ArrayList;
     13 import java.util.Iterator;
     14 import java.util.List;
     15 import java.util.Map;
     16 
     17 import org.apache.log4j.Logger;
     18 
     19 public class FileUtil
     20 {
     21     static Logger logger;
     22     
     23     static {
     24         FileUtil.logger = Logger.getLogger((Class)FileUtil.class);
     25     }
     26     public static <T> T readFile(final String filePath, T container, final String encode) {
     27         try {
     28             final File file = new File(filePath);
     29             if (!file.exists()) {
     30                 FileUtil.logger.info((Object)("file " + filePath + " does not exists."));
     31                 return container;
     32             }
     33             final FileInputStream fis = new FileInputStream(file);
     34             final InputStreamReader isr = new InputStreamReader(fis, (encode == null) ? "GBK" : encode);
     35             final BufferedReader br = new BufferedReader(isr);
     36             if (container instanceof List) {
     37                 final List list = (List)container;
     38                 String string = null;
     39                 while ((string = br.readLine()) != null) {
     40                     list.add(string);
     41                 }
     42             }
     43             else if (container instanceof String[]) {
     44                 String string2 = null;
     45                 final List<String> list2 = new ArrayList<String>();
     46                 while ((string2 = br.readLine()) != null) {
     47                     list2.add(string2);
     48                 }
     49                 final int count = list2.size();
     50                 final String[] strings = new String[count];
     51                 for (int i = 0; i < count; ++i) {
     52                     strings[i] = list2.get(i);
     53                 }
     54                 container = (T)(Object)strings;
     55             }
     56             else if (container instanceof Map) {
     57                 String string2 = null;
     58                 final Map map = (Map)container;
     59                 while ((string2 = br.readLine()) != null) {
     60                     final int a = string2.indexOf("=");
     61                     if (a != -1) {
     62                         map.put(string2.substring(0, a), string2.substring(a + 1));
     63                     }
     64                 }
     65             }
     66             else if (container instanceof byte[]) {
     67                 container = (T)getInputStreamBytes(fis);
     68             }
     69             br.close();
     70             isr.close();
     71             fis.close();
     72         }
     73         catch (Exception e1) {
     74             FileUtil.logger.error((Object)"", (Throwable)e1);
     75         }
     76         return container;
     77     }
     78     
     79     public static <T> T readFile(final InputStream is, T container, final String encode) {
     80         try {
     81             final InputStreamReader isr = new InputStreamReader(is, (encode == null) ? "GBK" : encode);
     82             final BufferedReader br = new BufferedReader(isr);
     83             if (container instanceof List) {
     84                 final List list = (List)container;
     85                 String string = null;
     86                 while ((string = br.readLine()) != null) {
     87                     list.add(string);
     88                 }
     89             }
     90             else if (container instanceof String[]) {
     91                 String string2 = null;
     92                 final List<String> list2 = new ArrayList<String>();
     93                 while ((string2 = br.readLine()) != null) {
     94                     list2.add(string2);
     95                 }
     96                 final int count = list2.size();
     97                 final String[] strings = new String[count];
     98                 for (int i = 0; i < count; ++i) {
     99                     strings[i] = list2.get(i);
    100                 }
    101                 container = (T)(Object)strings;
    102             }
    103             else if (container instanceof Map) {
    104                 String string2 = null;
    105                 final Map map = (Map)container;
    106                 while ((string2 = br.readLine()) != null) {
    107                     final int a = string2.indexOf("=");
    108                     if (a != -1) {
    109                         map.put(string2.substring(0, a), string2.substring(a + 1));
    110                     }
    111                 }
    112             }
    113             else if (container instanceof byte[]) {
    114                 container = (T)getInputStreamBytes(is);
    115             }
    116             br.close();
    117             isr.close();
    118             is.close();
    119         }
    120         catch (Exception e1) {
    121             FileUtil.logger.error((Object)"", (Throwable)e1);
    122         }
    123         return container;
    124     }
    125     
    126     public static void writeFile(final String directory, final String filename, final String encode, final Object... objs) {
    127         try {
    128             if (objs != null && objs.length > 0) {
    129                 final File file = new File(directory);
    130                 if (!file.exists()) {
    131                     file.mkdirs();
    132                 }
    133                 final FileOutputStream fos = new FileOutputStream(String.valueOf(directory) + "/" + filename);
    134                 final OutputStreamWriter osw = new OutputStreamWriter(fos, (encode == null) ? "GBK" : encode);
    135                 final BufferedWriter bw = new BufferedWriter(osw);
    136                 if (objs.length > 1) {
    137                     for (int len = objs.length, i = 0; i < len; ++i) {
    138                         final Object object = objs[i];
    139                         bw.write((object == null) ? "" : object.toString());
    140                         if (i != len - 1) {
    141                             bw.newLine();
    142                         }
    143                     }
    144                 }
    145                 else if (objs[0] instanceof List) {
    146                     final List list = (List)objs[0];
    147                     final Iterator iterator = list.iterator();
    148                     Object object = null;
    149                     while (true) {
    150                         if (iterator.hasNext()) {
    151                             object = iterator.next();
    152                             bw.write((object == null) ? "" : object.toString());
    153                         }
    154                         if (!iterator.hasNext()) {
    155                             break;
    156                         }
    157                         bw.newLine();
    158                     }
    159                 }
    160                 else if (objs[0] instanceof String[]) {
    161                     final String[] objects = (String[])objs[0];
    162                     for (int len2 = objects.length, j = 0; j < len2; ++j) {
    163                         final String object2 = objects[j];
    164                         bw.write((object2 == null) ? "" : object2);
    165                         if (j != len2 - 1) {
    166                             bw.newLine();
    167                         }
    168                     }
    169                 }
    170                 else if (objs[0] instanceof Map) {
    171                     final Map map = (Map)objs[0];
    172                     final int size = map.size();
    173                     int count = 0;
    174                     for (final Object key : map.keySet()) {
    175                         final Object value = map.get(key);
    176                         bw.write(String.valueOf((key == null) ? "" : key.toString()) + "=" + ((value == null) ? "" : value.toString()));
    177                         if (++count != size) {
    178                             bw.newLine();
    179                         }
    180                     }
    181                 }
    182                 else if (objs[0] instanceof byte[]) {
    183                     final byte[] bs = (byte[])objs[0];
    184                     fos.write(bs);
    185                 }
    186                 else {
    187                     bw.write(objs[0].toString());
    188                 }
    189                 bw.close();
    190                 osw.close();
    191                 fos.close();
    192             }
    193         }
    194         catch (Exception e) {
    195             FileUtil.logger.error((Object)"", (Throwable)e);
    196         }
    197     }
    198     
    199     public static <T> T readFileSafely(final String filePath, final T container) {
    200         final String encode = getEncoding(filePath);
    201         return readFile(filePath, container, encode);
    202     }
    203     
    204     public static void repOldStr(final File file, final String oldStr, final String newStr) {
    205         final String encode = getEncoding(file.getPath());
    206         final List<String> list = readFile(file.getPath(), new ArrayList<String>(), encode);
    207         final List<String> list2 = new ArrayList<String>();
    208         for (String str : list) {
    209             str = str.replace(oldStr, newStr);
    210             list2.add(str);
    211         }
    212         writeFile(file.getParent(), file.getName(), encode, list2);
    213     }
    214     
    215     private static byte[] getInputStreamBytes(final InputStream is) throws IOException {
    216         final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    217         final byte[] buffer = new byte[1024];
    218         int len = 0;
    219         while ((len = is.read(buffer)) != -1) {
    220             outStream.write(buffer, 0, len);
    221         }
    222         final byte[] data = outStream.toByteArray();
    223         return data;
    224     }
    225     
    226     public static boolean appendFile(final String filepath, final String... content) {
    227         if (content == null || content.length == 0) {
    228             return true;
    229         }
    230         try {
    231             final FileWriter fw = new FileWriter(filepath, true);
    232             for (int i = 0; i < content.length; ++i) {
    233                 final String s = content[i];
    234                 fw.write(String.valueOf(s) + "
    ");
    235             }
    236             fw.close();
    237             return true;
    238         }
    239         catch (Exception e) {
    240             FileUtil.logger.error((Object)"", (Throwable)e);
    241             return false;
    242         }
    243     }
    244     public static String getEncoding(final String filepath) {
    245         return EncodingDetect.getJavaEncode(filepath);
    246     }
    247     public static void main(String[] args) {  
    248         String file = "C:\Users\lenovo\Downloads\a.log";  
    249         String encode=EncodingDetect.getJavaEncode(file);  
    250         System.out.println(encode);  
    251     }  
    252 }
    View Code
    这个博客主要是javaEE相关或者不相关的记录, hadoop与spark的相关文章我写在下面地址的博客啦~ http://www.cnblogs.com/sorco
  • 相关阅读:
    java Thread之ThreadLocal(线程局部变量)
    java设计模式之接口隔离原则(ISP)
    java设计模式之开放关闭原则(OCP)
    java设计模式之迪米特法则(LoD)
    java设计模式之单一职责原则(SRP)
    android点滴(25)之 originalpackage
    VC 注册表操作
    java设计模式之依赖倒置原则(DIP)
    DFT 离散傅里叶变换 与 补零运算
    序列循环移位
  • 原文地址:https://www.cnblogs.com/orco/p/6236757.html
Copyright © 2011-2022 走看看