Advertisement
phl0w

Untitled

Jul 31st, 2012
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. import java.io.*;
  2. import java.text.DateFormat;
  3. import java.text.SimpleDateFormat;
  4. import java.util.*;
  5.  
  6. public class Chatlogs {
  7.  
  8.     private static final DateFormat FORMAT = new SimpleDateFormat("yyyy MMM dd hh:mm:sss");
  9.  
  10.     public static void main(String... args) {
  11.         try {
  12.             BufferedWriter bw;
  13.             bw = new BufferedWriter(new FileWriter("C:/java/writetohere.txt"));
  14.             bw.write(readFile(new File("C:/java/readfromhere.txt").getAbsolutePath()));
  15.             bw.close();
  16.         } catch (IOException e) {
  17.             e.printStackTrace();
  18.         }
  19.     }
  20.  
  21.     private static String readFile(String path) throws IOException {
  22.         ArrayList<String> list = new ArrayList<String>();
  23.         FileInputStream fstream = new FileInputStream(path);
  24.         DataInputStream in = new DataInputStream(fstream);
  25.         BufferedReader br = new BufferedReader(new InputStreamReader(in));
  26.         String strLine;
  27.         StringBuilder sb = new StringBuilder();
  28.         while ((strLine = br.readLine()) != null) {
  29.             if (!strLine.contains("**** LOGGEN") && !strLine.contains("**** LOGGING") && !strLine.equals("")) {
  30.                 list.add(strLine);
  31.             }
  32.         }
  33.         br.close();
  34.         in.close();
  35.         fstream.close();
  36.         List<LogEntryComparator> entries = new LinkedList<LogEntryComparator>();
  37.         Date date = null;
  38.         for (String line : list) { //list is an arraylist of strings.
  39.             String time = "2011 " + line.substring(0, 15);
  40.             try {
  41.                 date = FORMAT.parse(time);
  42.             } catch (Exception e) {
  43.                 e.printStackTrace();
  44.             }
  45.             entries.add(new LogEntryComparator(date, line.substring(15)));
  46.         }
  47.         Collections.sort(entries);
  48.         for (int i = 0; i < entries.size(); i++) {
  49.             System.out.println(entries.get(i).time + entries.get(i).entry);
  50.         }
  51.         return sb.toString();
  52.  
  53.     }
  54. }
  55.  
  56.  
  57. readfromhere.txt:
  58. jul 31 17:09:50 <phl0w> tes
  59. jul 31 17:10:49 <Andy_> Speed
  60. jul 31 17:10:51 <Andy_> lol Speed
  61. jul 31 19:20:50 <phl0w> lolfile1
  62. jul 29 17:09:50 <phl0w> tes
  63. jul 31 17:08:49 <yyyy> alewrwae
  64. jul 31 17:11:51 <xxxx> wrkjaer
  65. jul 31 19:20:53 <phl0w> lolfile3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement