Skip to content

Instantly share code, notes, and snippets.

@pxpc2
Created August 8, 2012 01:05
Show Gist options
  • Select an option

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

Select an option

Save pxpc2/3291121 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 = 0; i < getFiles(DataValues.PATH).length; i++)
{
DataValues.files.add(getFiles(DataValues.PATH)[i]);
if (DataValues.files.get(i).getName() == DataValues.files.get(i+1).getName())
{
if (getTimeInDate(DataValues.files.get(i)).after(getTimeInDate(DataValues.files.get(i+1))))
{
List<String> fileOne = getContent(DataValues.files.get(i));
List<String> fileTwo = getContent(DataValues.files.get(i));
for (String s : fileOne)
{
for (String ss : fileTwo)
{
if (s.equals(ss))
{
fileTwo.remove(ss);
}
}
}
try
{
fstream = new FileWriter(DataValues.files.get(i));
out = new BufferedWriter(fstream);
for (String s : fileTwo)
{
out.write(s + "\n");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
}
// Methods
private static List<String> getContent(File file)
{
List<String> lines = new ArrayList<String>();
String line = null;
StringBuilder header = new StringBuilder();
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>());
}
private File getFile(String path)
{
return new File(path+".txt");
}
private String getPath(File file)
{
return file.getAbsolutePath()+file.getName()+".txt";
}
private static File[] getFiles(String path)
{
return new File(path).listFiles();
}
private static Date getTimeInDate(File file)
{
String currentHeader = null;
StringBuilder header = new StringBuilder();
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();
}
// 30 31 // 42 43 44 45
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 final String PATH =
(System.getProperty("os.name").contains("Windows") ? "Drew/AppData/Roaming/X-Chat 2/xchatlogs"
: "Drew/.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