Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.logging.Logger;
- public class Merge {
- private static final Logger log = Logger.getLogger(Merge.class.getName());
- 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"));
- private static final HashMap<String, File[]> files = new HashMap<String, File[]>();
- private static final HashMap<File, File[]> filesToWrite = new HashMap<File, File[]>();
- public static void main(String... args) {
- long startTime = System.currentTimeMillis();
- if (ROOT_FOLDER.exists()) {
- for (final File f : ROOT_FOLDER.listFiles()) {
- if (f.isDirectory() && f.getName().contains("System")) { //mandatory check
- for (final File sub : f.listFiles()) {
- String channelName = (!sub.getName().contains("#") ? "privmsg-" : "") + sub.getName().split("-")[1].replaceAll(".log", "");
- if (files.containsKey(channelName)) {
- ArrayList<File> tempFiles = new ArrayList<File>();
- for (File t : files.get(channelName)) {
- tempFiles.add(t);
- }
- tempFiles.add(sub);
- File[] array = new File[tempFiles.size()];
- array = tempFiles.toArray(array);
- files.put(channelName, array);
- } else {
- files.put(channelName, new File[]{sub});
- }
- }
- }
- }
- } else {
- log.info("No log folder detected.");
- }
- String channel;
- File f;
- for (Map.Entry<String, File[]> es : files.entrySet()) {
- channel = es.getKey();
- f = new File(ROOT_FOLDER.getAbsolutePath() + "/merged-" + channel + ".log");
- try {
- if (f.exists()) {
- if (f.delete()) {
- f.createNewFile();
- }
- }
- } catch (IOException ioe) {
- ioe.printStackTrace();
- }
- filesToWrite.put(f, es.getValue());
- }
- try {
- BufferedWriter bw;
- for (Map.Entry<File, File[]> entry : filesToWrite.entrySet()) {
- bw = new BufferedWriter(new FileWriter(entry.getKey()));
- for (File read : entry.getValue()) {
- bw.write(readFile(read.getAbsolutePath()));
- }
- bw.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("That took " + (System.currentTimeMillis() - startTime) + " ms.");
- }
- private static String readFile(String path) throws IOException {
- FileInputStream fstream = new FileInputStream(path);
- DataInputStream in = new DataInputStream(fstream);
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- String strLine;
- StringBuilder sb = new StringBuilder();
- while ((strLine = br.readLine()) != null) {
- if (!strLine.contains("**** LOGGEN") && !strLine.contains("**** LOGGING") && !strLine.equals("")) {
- sb.append(strLine).append("\n");
- }
- }
- br.close();
- in.close();
- fstream.close();
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement