1 20 21 26 import java.io.File ; 27 import java.util.Vector ; 28 class MyUtility { 29 private static String osName = System.getProperty("os.name").toLowerCase(); 30 private static File roots[] = null; 31 public static String length2KB(long length) { 32 long KB = 1024; 33 long MB = 1024 * 1024; 34 35 if (length == 0) { 36 return String.valueOf("0 KB "); 37 } 38 39 String kbStr = ""; 40 long mbCount = length / MB; 41 42 long kbCount = ((length - mbCount * MB) + 1023) / KB; 43 44 if (mbCount != 0) { 45 kbStr += String.valueOf(mbCount + ","); 46 } 47 48 if (kbCount == 0) { 49 if (mbCount == 0) { 50 kbStr += String.valueOf("0 KB "); 51 } else { 52 kbStr += String.valueOf("000 KB "); 53 } 54 } else { 55 kbStr += String.valueOf(kbCount + " KB "); 56 } 57 58 return kbStr; 59 } 60 61 static class FileSystemRoot extends File { 62 public FileSystemRoot(File f) { 63 super(f, ""); 64 } 65 66 public FileSystemRoot(String s) { 67 super(s); 68 } 69 70 public boolean isDirectory() { 71 return true; 72 } 73 } 74 75 public static File [] getRoots() { 76 if (roots == null) { 77 constructRoots(); 78 } 79 return roots; 80 } 81 82 private static void constructRoots() { 83 if (osName.startsWith("windows")) { 84 Vector rootsVector = new Vector (); 85 86 FileSystemRoot floppy = new FileSystemRoot("A" + ":" + "\\"); 88 rootsVector.addElement(floppy); 89 90 for (char c = 'C'; c <= 'Z'; c++) { 93 char device[] = {c, ':', '\\'}; 94 String deviceName = new String (device); 95 File deviceFile = new FileSystemRoot(deviceName); 96 if (deviceFile != null && deviceFile.exists()) { 97 rootsVector.addElement(deviceFile); 98 } 99 } 100 roots = new File [rootsVector.size()]; 101 rootsVector.copyInto(roots); 102 } else { 103 roots = File.listRoots(); 104 } 105 } 106 } 107 | Popular Tags |