import java.util.*; import java.util.zip.*; import java.util.List; import java.util.regex.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import javax.swing.table.*; import java.io.*; import java.net.*; import java.lang.reflect.*; import java.lang.ref.*; import java.lang.management.*; import java.security.*; import java.security.spec.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import java.math.*; public class main { public static void main(String[] args) throws Exception { String dirToList = "."; File d = new File(dirToList); if (d.isDirectory()) { File[] files = d.listFiles(); sortFilesByDate(files); print(listSomeFiles(files)); } } static volatile StringBuffer local_log = new StringBuffer(); // not redirected static volatile StringBuffer print_log = local_log; // might be redirected, e.g. to main bot // in bytes - will cut to half that static volatile int print_log_max = 1024*1024; static volatile int local_log_max = 100*1024; static void print() { print(""); } // slightly overblown signature to return original object... static A print(A o) { String s = String.valueOf(o) + "\n"; StringBuffer loc = local_log; StringBuffer buf = print_log; int loc_max = print_log_max; if (buf != loc && buf != null) { print_append(buf, s, print_log_max); loc_max = local_log_max; } if (loc != null) print_append(loc, s, loc_max); System.out.print(s); return o; } static void print(long l) { print(String.valueOf(l)); } static void print_append(StringBuffer buf, String s, int max) { synchronized(buf) { buf.append(s); max /= 2; if (buf.length() > max) try { int newLength = max/2; int ofs = buf.length()-newLength; String newString = buf.substring(ofs); buf.setLength(0); buf.append("[...] ").append(newString); } catch (Exception e) { buf.setLength(0); } } } static String listSomeFiles(File[] files) { return listSomeFiles(asList(files)); } static String listSomeFiles(List files) { StringBuilder buf = new StringBuilder(); for (File f : files) { boolean isDir = f.isDirectory(); String line = (isDir ? "D" : "F") + " " + f.getName(); if (!isDir) { long size = f.length(); String suffix = "B"; if (size >= 1024*1024) { size = (size+1024*1024-1) / (1024*1024); suffix = "MB"; } else if (size >= 1024) { size = (size+1024-1) / 1024; suffix = "KB"; } line += " (" + size + " " + suffix + ")"; } buf.append(line).append("\n"); } return str(buf); } // newest last static void sortFilesByDate(File[] files) { sort(files, new Comparator(){ public int compare(File a, File b) { long xx = a.lastModified(), yy = b.lastModified(); return xx < yy ? -1 : xx == yy ? 0 : 1; } }); } static String str(Object o) { return String.valueOf(o); } static void sort(T[] a, Comparator c) { Arrays.sort(a, c); } static void sort(List a, Comparator c) { Collections.sort(a, c); } static ArrayList asList(A[] a) { return new ArrayList(Arrays.asList(a)); } static ArrayList asList(int[] a) { ArrayList l = new ArrayList(); for (int i : a) l.add(i); return l; } static ArrayList asList(Collection s) { return s == null ? new ArrayList() : s instanceof ArrayList ? (ArrayList) s : new ArrayList(s); } }