Skip to content

Instantly share code, notes, and snippets.

@pxpc2
Created August 12, 2012 15:36
Show Gist options
  • Select an option

  • Save pxpc2/3332338 to your computer and use it in GitHub Desktop.

Select an option

Save pxpc2/3332338 to your computer and use it in GitHub Desktop.
package stages.two;
import java.io.*;
import java.util.*;
/**
* @author pxpc2
*/
public class LogMerger {
private static FileWriter fstream;
private static BufferedWriter out;
public static void main(String... args) {
System.out.println("Log Merger Starting");
for (int i : DataValues.systems) {
for (File f : getFiles(DataValues.PATH+" "+DataValues.systems[i])) {
DataValues.files.add(f);
}
}
for (int i = 0; i < DataValues.files.size(); i++) {
if (i != DataValues.files.size() - 1) {
File currentFile = DataValues.files.get(i);
for (File f : DataValues.files) {
if (f.getName().equals(currentFile.getName())) {
List<String> fContent = getContent(f);
DataValues.files.remove(f);
for (String s : fContent) {
for (String s2 : getContent(currentFile)) {
if (!s.equals(s2))
addToFile(currentFile, s);
}
}
}
}
}
else {
for (File f : DataValues.files) {
createFile(DataValues.PATH+" "+4, getContent(f));
}
}
}
}
// Methods
/**
*
* @param path path to create the file
* @param content the content to add to the file
*/
private static void createFile(String path, List<String> content) {
}
/**
*
* @param file the file
* @param s string to add to the file
*/
private static void addToFile(File file, String s) {
try {
fstream = new FileWriter(file);
out = new BufferedWriter(fstream);
out.write(s + "\n");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @param file the file to get
* @return all the lines in the file (in a List<String>)
*/
private static List<String> getContent(File file) {
List<String> lines = new ArrayList<String>();
String line = null;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
while ((line = reader.readLine()) != null) {
if (!line.contains("**** BEGIN")) {
lines.add(line);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Stopping");
}
return (lines != null ? lines : new ArrayList<String>());
}
/**
*
* @param path path to get files from
* @return all the files in the path
*/
private static File[] getFiles(String path) {
return new File(path).listFiles();
}
/**
*
* @param file gets the time of the file
* @return
*/
private static Date getTimeInDate(File file) {
String currentHeader = null;
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(file));
String currentLine = null;
while ((currentLine = reader.readLine()) != null) {
if (currentLine.contains("**** BEGIN")) {
currentHeader = currentLine;
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
String day = currentHeader.substring(22, 24);
String month = currentHeader.substring(26, 28);
int dayOfMonth = Integer.parseInt(currentHeader.substring(30, 31).trim());
int year = Integer.parseInt(currentHeader.substring(42, 45));
return convert(day, month, dayOfMonth, year).getTime();
}
/**
*
* @param day the day
* @param month the month
* @param dayOfMonth day of month (1-31)
* @param year the year
* @return a GregorianCalendar
*/
public static GregorianCalendar convert(String day, String month, int dayOfMonth, int year) {
GregorianCalendar c = new GregorianCalendar();
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
c.set(Calendar.YEAR, year);
String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; // 26, 27, 28
String[] days = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; // 22, 23, 24
for (int i = 0; i < months.length; i++) {
if (months[i].toLowerCase().startsWith(month.toLowerCase())) {
c.set(Calendar.MONTH, i);
break;
}
}
for (int i = 0; i < days.length; i++) {
if (days[i].toLowerCase().startsWith(day.toLowerCase())) {
c.set(Calendar.DAY_OF_WEEK, i);
break;
}
}
return c;
}
public boolean isAfter(GregorianCalendar first, GregorianCalendar second) {
return first.after(second);
}
/**
* @author pxpc2
*/
public static class DataValues {
public static int[] systems = {0, 1, 2, 3};
public static final String PATH =
(System.getProperty("user.home") + (System.getProperty("os.name").contains("Windows") ? "/AppData/Roaming/X-Chat 2/xchatlogs"
: "/.xchat2/xchatlogs"));
public static ArrayList<File> files = new ArrayList<File>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment