`
yangzb
  • 浏览: 3473255 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Commons Virtual File System

    博客分类:
  • Java
阅读更多
VFS

Commons Virtual File System (VFS)提供了一种能够统一访问不同文件系统的抽象层。这个组件能够配置为同时连接一个或多个的文件系统。在Linux操作系统下也是比较容易的。

VFS支持下列文件系统:

  • Local files – 本地文件和文件夹(file://)
  • Zip, jar, tar, tgz, tbz2, gzip, bzip2 – 不同的压缩格式(zip://, jar://, etc.)
  • CIFS – Samba服务或Windows共享(smb://)
  • FTP – FTP服务器(ftp://)
  • HTTP和HTTPS (http://)
  • SFTP – SSH或SCP服务器(sftp://)
  • Temporary files (tmp://)
  • WebDAV – Web-based Distributed Authoring and Versioning (webdav://)
  • Resource from class loader – 使用ClassLoader获取类或其他资源(res://)

        这个组件对那些需要访问不同类型的文件系统的应用程序来说十分有用。举例来说:一个桌面搜索工具同这个框架非常类似。它提供用户搜索文件或文件的内容。另外一个例子就是集成Windows IE浏览器类似的功能到Java应用程序。

        范例应用程序是一个工具,它使用Commons VFS来在文件夹中查询Zip和Jar文件。应用程序没有提供用户界面,但是提供了一个测试用例来证明它能良好的工作。可以在in.co.narayanan.commons.vfs 找到范例和测试代码。按顺序运行范例应用程序,下载源码压缩包,运行Ant构建脚本来创建Commons VFS库。Ant脚本会自动下载其他依赖的库文件。最后通过JUnit测试来启动范例应用程序。[in.co.narayanan.commons.vfs.TestSearchBuddy ]

        最初的使用Commons VFS的想法是创建一个提供支持每种文件类型并能够由DefautFileSystemManager引用的Manager实例。为深层次的操作,需要通 过manager的resolveFile方法获取FileObject实例。Manager和FileObject提供了不同的方法,可以在 JavaDoc中找到他们的详细说明。下一段文字描述在搜索工具中如何使用Commons VFS API。

        清单6通过in.co.narayanan.commons.vfs.SearchBuddy类中初始化DefaultFileSystemManager类的代码片断。
 
清单6.初始化文件系统管理器
/**
 * Initialize the DefaultFileSystemManager to support
 * file, zip and jar providers. A virtual file system
 * is created and passed to the SearchableVirtualFileSystem
 * decorator class.
 *
 * @throws SearchException Error in initializing the file
 *       FileSystemManager
 */
private void init() throws SearchException {
 defFileSysMgr = new DefaultFileSystemManager();
 
 try {
 
  defFileSysMgr.addProvider("file", new DefaultLocalFileProvider());
  defFileSysMgr.addProvider("zip", new ZipFileProvider());
  defFileSysMgr.addProvider("jar", new JarFileProvider());
  defFileSysMgr.init();
 
  // 创建虚拟文件系统
  VirtualFileSystem vfs =
  (VirtualFileSystem)defFileSysMgr.createVirtualFileSystem("vfs://").getFileSystem();
 
  searchableVfs = new SearchableVirtualFileSystem(vfs);
   
 } catch (FileSystemException e) {
  throw new SearchException("Unable to initialize the FileSystemManager.", e);
 }
}
        高亮代码行为在文件系统中查询本地普通文件、zip文件、jar文件增加providers。这段代码创建一个VirtualFileSystem类的实例,这个类可以用来装备其它的文件系统。

        清单7时测试用例类TestSearchBuddy的代码片断,它说明范例应用程序如合查找文件。


 
清单7. 使用查询工具
/**
 * Adds the folder, zip, and a jar file to search
 *
 * @throws Exception Error in the test.
 */
public void testSearchInZips() throws Exception {
 SearchBuddy searchTool = new SearchBuddy();
 searchTool.addSearchableZip("testroot.zip");
 searchTool.addSearchableJar("testjar.jar");
 searchTool.addSearchableFolder(".");
  
 System.out.println("Searching for news.txt");
 searchTool.search("news", "txt");
  
 System.out.println("Searching for Range.class");
 searchTool.search("range", "class");
  
 System.out.println("Searching for test.xml");
 searchTool.search("test", "xml");
 
 System.out.println("Searching for *.properties");
 searchTool.search(null, "properties");
 searchTool.close();

}
 
        高亮部分代码行增加zip、jar文件到searchTool中。search方法通过加入要进行查询的文件名、扩展名来进行查询操作。

分享到:
评论
1 楼 hsbcnet 2011-07-24  

  LZ你好,我想写个查看远程文件的小工具,刚看到vfs的介绍。
  其实我想做到的是类似在putty登录服务器之后tail -f 日志文件,我的工具能源源不断的收到日志文件的更新,然后我要逐行逐行处理。
  请问vfs能做到我的要求吗?谢谢。

相关推荐

Global site tag (gtag.js) - Google Analytics