Advertisement
phl0w

Untitled

Jul 31st, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.logging.Logger;
  6.  
  7. public class Merge {
  8.  
  9.     private static final Logger log = Logger.getLogger(Merge.class.getName());
  10.     private static final File ROOT_FOLDER = new File(System.getProperty("user.home") + (System.getProperty("os.name").contains("Windows") ? "/AppData/Roaming/X-Chat 2/xchatlogs" : "/.xchat2/xchatlogs"));
  11.     private static final HashMap<String, File[]> files = new HashMap<String, File[]>();
  12.     private static final HashMap<File, File[]> filesToWrite = new HashMap<File, File[]>();
  13.  
  14.     public static void main(String... args) {
  15.         long startTime = System.currentTimeMillis();
  16.         if (ROOT_FOLDER.exists()) {
  17.             for (final File f : ROOT_FOLDER.listFiles()) {
  18.                 if (f.isDirectory() && f.getName().contains("System")) { //mandatory check
  19.                     for (final File sub : f.listFiles()) {
  20.                         String channelName = (!sub.getName().contains("#") ? "privmsg-" : "") + sub.getName().split("-")[1].replaceAll(".log", "");
  21.                         if (files.containsKey(channelName)) {
  22.                             ArrayList<File> tempFiles = new ArrayList<File>();
  23.                             for (File t : files.get(channelName)) {
  24.                                 tempFiles.add(t);
  25.                             }
  26.                             tempFiles.add(sub);
  27.                             File[] array = new File[tempFiles.size()];
  28.                             array = tempFiles.toArray(array);
  29.                             files.put(channelName, array);
  30.                         } else {
  31.                             files.put(channelName, new File[]{sub});
  32.                         }
  33.                     }
  34.                 }
  35.             }
  36.         } else {
  37.             log.info("No log folder detected.");
  38.         }
  39.         String channel;
  40.         File f;
  41.         for (Map.Entry<String, File[]> es : files.entrySet()) {
  42.             channel = es.getKey();
  43.             f = new File(ROOT_FOLDER.getAbsolutePath() + "/merged-" + channel + ".log");
  44.             try {
  45.                 if (f.exists()) {
  46.                     if (f.delete()) {
  47.                         f.createNewFile();
  48.                     }
  49.                 }
  50.             } catch (IOException ioe) {
  51.                 ioe.printStackTrace();
  52.             }
  53.             filesToWrite.put(f, es.getValue());
  54.         }
  55.         try {
  56.             BufferedWriter bw;
  57.             for (Map.Entry<File, File[]> entry : filesToWrite.entrySet()) {
  58.                 bw = new BufferedWriter(new FileWriter(entry.getKey()));
  59.                 for (File read : entry.getValue()) {
  60.                     bw.write(readFile(read.getAbsolutePath()));
  61.                 }
  62.                 bw.close();
  63.             }
  64.         } catch (IOException e) {
  65.             e.printStackTrace();
  66.         }
  67.         System.out.println("That took " + (System.currentTimeMillis() - startTime) + " ms.");
  68.     }
  69.  
  70.     private static String readFile(String path) throws IOException {
  71.         FileInputStream fstream = new FileInputStream(path);
  72.         DataInputStream in = new DataInputStream(fstream);
  73.         BufferedReader br = new BufferedReader(new InputStreamReader(in));
  74.         String strLine;
  75.         StringBuilder sb = new StringBuilder();
  76.         while ((strLine = br.readLine()) != null) {
  77.             if (!strLine.contains("**** LOGGEN") && !strLine.contains("**** LOGGING") && !strLine.equals("")) {
  78.                 sb.append(strLine).append("\n");
  79.             }
  80.         }
  81.         br.close();
  82.         in.close();
  83.         fstream.close();
  84.         return sb.toString();
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement