日期:2014-05-16  浏览次数:21082 次

apache.commons.io 笔记1

看看,常见的东西都有了,如查询盘的剩余空间,文件夹大小,文件名处理,文件排序,读写文件……

我用的1.4版本。

public static void main(String[] args) throws IOException {
//   org.apache.commons.io.comparator.PathFileComparator
//   org.apache.commons.io.comparator.LastModifiedFileComparator
//   org.apache.commons.io.comparator.SizeFileComparator
  
//   org.apache.commons.io.filefilter.SuffixFileFilter
  
//   org.apache.commons.io.input.NullInputStream
//   org.apache.commons.io.input.AutoCloseInputStream
  
//   org.apache.commons.io.FilenameUtils.getExtension(filename)
//   org.apache.commons.io.FilenameUtils.removeExtension(filename)
//   org.apache.commons.io.FilenameUtils.getFullPathNoEndSeparator(filename)
//   org.apache.commons.io.FilenameUtils.indexOfExtension(filename)
  
   long f = org.apache.commons.io.FileSystemUtils.freeSpaceKb("c:");
   System.out.println(f);
  
   //org.apache.commons.io.FileSystemUtils里面有个演示了正确执行 cmd的方式,List performCommand(String[] cmdAttribs, int max) throws IOException {
}


public static void test_FileUtils() throws IOException {
   long begin = System.currentTimeMillis();
//   String content = org.apache.commons.io.FileUtils.readFileToString(new File("c:/a.txt"), "UTF-8");

//   org.apache.commons.io.FileUtils.writeByteArrayToFile(file, data)
//   org.apache.commons.io.FileUtils.cleanDirectory(arg0)
//   org.apache.commons.io.FileUtils.copyDirectory(srcDir, destDir)
//   org.apache.commons.io.FileUtils.copyURLToFile(arg0, arg1)
//   org.apache.commons.io.FileUtils.copyFileToDirectory(srcFile, destDir)
//   org.apache.commons.io.FileUtils.lineIterator(file)
//   org.apache.commons.io.FileUtils.moveDirectory(srcDir, destDir)
//   org.apache.commons.io.FileUtils.moveFile(srcFile, destFile)
   //设置最后修改时间为当前时间
//   org.apache.commons.io.FileUtils.touch(arg0)
//   org.apache.commons.io.FileUtils.writeLines(file, lines)
//   org.apache.commons.io.FileUtils.writeStringToFile(file, data, encoding)

   //第一次58秒,第二次6s
   long dirsize = org.apache.commons.io.FileUtils.sizeOfDirectory(new File("I:/文档 照片/照片"));

   long end = System.currentTimeMillis();
   System.out.println(dirsize + " " + ((end - begin)/1000) + "s");

   FileUtils.checksumCRC32(file);
   System.out.println(FileUtils.byteCountToDisplaySize(584235));//显示为多少M或多少K
    //FileUtils.contentEquals(file1, file2)
   FileUtils.cleanDirectory(directory);
   FileUtils.writeStringToFile(file, data, encoding);

   File destination=new File("c:/new.html");
   URL source = new URL("http://news.baidu.com/");
   FileUtils.copyURLToFile(source, destination);

   FileUtils.isFileNewer(file, date);
   FileUtils.isFileOlder(file, reference);
   FileUtils.isFileOlder(file, timeMillis);
   FileUtils.iterateFiles(directory, fileFilter, dirFilter);

   LineIterator i = FileUtils.lineIterator(new File("c:/abcd.txt"));
   while(i.hasNext()){
   System.out.println(i.nextLine());
   }

   FileUtils.listFiles(directory, extensions, recursive);
   FileUtils.listFiles(directory, fileFilter, dirFilter);
   FileUtils.readFileToString(file);
   System.out.println(FileUtils.sizeOfDirectory(new File("E:/photo")));//累计法得
   System.out.println(FileSystemUtils.freeSpaceKb("E:")+"k");
   System.out.println(freeSpaceWindows("E:"));
}
?