Apache commons VFS简介和ShowProperties源代码示例

Apache commons VFS又叫做 Apache Commons Virtual FileSystem。是一组功能强大的对各类资源的访问接口,目前这个JAR包得到了全新的重构,目前最新的版本是2.2。

如果我们在平常的工作中,需要得到一些不同格式文件的信息,比如文件大小、所在路径、文件最后更改时间等,或者我们需要对文件进行一些常规的操作,比如删除文件,拷贝文件等等,那么Apache Commons中的VFS(Virtual File System)就是我们可以考虑的一个开源系统。

据VFS的官网介绍,它目前支持如下文件格式:

FTP 、Local Files 、HTTP and HTTPS 、SFTP 、Temporary Files 、Zip, Jar and Tar (uncompressed, tgz or tbz2) 、gzip and bzip2 、res 、ram 、mime。

它的官方网址为:http://commons.apache.org/vfs/

当我们需要去对文件操作的时候,应当首先想到VFS。

VFS2有一些自带的example,学习下:

package test.ffm83.commons.VFS;

import java.text.DateFormat;

import java.util.Date;

import org.apache.commons.vfs2.FileObject;

import org.apache.commons.vfs2.FileSystemException;

import org.apache.commons.vfs2.FileSystemManager;

import org.apache.commons.vfs2.FileType;

import org.apache.commons.vfs2.VFS;

/**

* 通过VFS获取文件一些信息

* 使用2.0版本实现

* @author示例,修改:范芳铭

*/

public
class
ShowProperties {

public
static void
main(final String[] args) {

Stringarg = "D:\\develop\\eclipse\\Workspaces\\test_all\\wx114_lib\\commons-io-2.4.jar";

try {

final FileSystemManager mgr =VFS.getManager();

System.out.println();

System.out.println("Parsing: "+ arg);

final FileObject file =mgr.resolveFile(arg);

System.out.println("URL: "+ file.getURL());

System.out.println("getName(): "+ file.getName());

System.out.println("BaseName: "+ file.getName().getBaseName());

System.out.println("Extension: "+ file.getName().getExtension());

System.out.println("Path: "+ file.getName().getPath());

System.out.println("Scheme: "+ file.getName().getScheme());

System.out.println("URI: "+ file.getName().getURI());

System.out.println("Root URI: "+ file.getName().getRootURI());

System.out.println("Parent: "+ file.getName().getParent());

System.out.println("Type: "+ file.getType());

System.out.println("Exists: "+ file.exists());

System.out.println("Readable: "+ file.isReadable());

System.out.println("Writeable: "+ file.isWriteable());

System.out.println("Root path: "

+file.getFileSystem().getRoot().getName().getPath());

if (file.exists()) {

if(file.getType().equals(FileType.FILE)) {

System.out.println("Size: "+ file.getContent().getSize()

+" bytes");

}else
if
(file.getType().equals(FileType.FOLDER)

&&file.isReadable()) {

final FileObject[] children =file.getChildren();

System.out.println("Directory with "+ children.length

+" files");

for (int iterChildren = 0;iterChildren < children.length; iterChildren++) {

System.out.println("#" + iterChildren +
": "

+children[iterChildren].getName());

if (iterChildren > 5) {

break;

}

}

}

System.out.println("Last modified: "

+DateFormat.getInstance().format(

new Date(file.getContent()

.getLastModifiedTime())));

}else{

System.out.println("The file does not exist");

}

file.close();

}catch(finalFileSystemException ex) {

ex.printStackTrace();

}

}

}

运行结果如下:

Parsing:D:\develop\eclipse\Workspaces\test_all\wx114_lib\commons-io-2.4.jar

URL:file:///D:/develop/eclipse/Workspaces/test_all/wx114_lib/commons-io-2.4.jar

getName():file:///D:/develop/eclipse/Workspaces/test_all/wx114_lib/commons-io-2.4.jar

BaseName: commons-io-2.4.jar

Extension: jar

Path:/develop/eclipse/Workspaces/test_all/wx114_lib/commons-io-2.4.jar

Scheme: file

时间: 2024-08-29 08:13:50

Apache commons VFS简介和ShowProperties源代码示例的相关文章

Apache commons VFS 文件操作 源代码示例

通过VFS对文件进行一些操作,包括写入.读取文件,判断文件是否可读可写等示例.Apache commons VFS包和apache commons的其他基础性类有非常密切的关系. 整理了一些常用的方法,源代码如下: package test.ffm83.commons.VFS; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.O

Apache Commons CLI 开发命令行工具示例

概念说明Apache Commons CLI 简介 虽然各种人机交互技术飞速发展,但最传统的命令行模式依然被广泛应用于各个领域:从编译代码到系统管理,命令行因其简洁高效而备受宠爱.各种工具和系统都 提供了详尽的使用手册,有些还提供示例说明如何二次开发.然而关于如何开发一个易用.强壮的命令行工具的文章却很少.本文将结合 Apache Commons CLI,通过一个完整的例子展示如何准备.开发.测试一个命令行工具.希望本文对有相关需求的读者能有所帮助.      Apache Commons CL

使用 Apache Commons CLI 开发命令行工具示例

概念说明 Apache Commons CLI 简介 Apache Commons CLI 是 Apache 下面的一个解析命令行输入的工具包,该工具包还提供了自动生成输出帮助文档的功能. Apache Commons CLI 支持多种输入参数格式,主要支持的格式有以下几种: POSIX(Portable Operating System Interface of Unix)中的参数形式,例如 tar -zxvf foo.tar.gz GNU 中的长参数形式,例如 du --human-read

Apache commons VFS之Shell源代码示例

这个示例主要是模拟一个Shell程序,通过输入一些命令,执行操作.源代码来自官网. 这个示例的特点: 1.       接收用户的输入: 2.       通过字符解析和判断: 3.       利用VFS自带的一些功能进行功能实现: package test.ffm83.commons.VFS; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import

Apache Commons IO简介

虽然Google的guava对Java的IO操作进行了一定封装,但是它更偏向于集合.并发和缓存,在实际项目中,我非常喜欢guava,同时我也非常喜欢Apache的一个工具包org.apache.commons.io,这两个工具包提供非常强大的工具能力,能够简化代码逻辑,提高开发效率和质量,是每个Java程序员都应该掌握的工具包.此文简单介绍一下org.apache.commons.io,详细的可参考其API注,此文绝大部分内容翻译自http://www.javacodegeeks.com/201

Apache Commons pool 简介和pool连接池代码

在实际中工作,我们经常遇到需要连接池的地方,特别是数据库连接池. 我们为什么需要池呢?因为这些资源的创建,都很消耗资源.因此,我们使用一个对象池,里面预先创建了一些资源对象.当我们需要时,从池中取出对象,而不需要时,把对象返回池中.这样就可以提高代码运行的效率. Apache Commons Pool(http://commons.apache.org/pool/)为我们提供了很方便的接口来实现对象池.我们唯一需要实现的就是如何产生对象,而不用去考虑一堆多线程问题. 2013年,Apache C

Apache Commons CLI 简介

CLI 命令代码实现 命令行程序处理流程相对比较简单,主要流程为设定命令行参数 -> 解析输入参数 -> 使用输入的数据进行逻辑处理CLI 定义阶段 每一条命令行都必须定义一组参数,它们被用来定义应用程序的接口.Apache Commons CLI 使用 Options 这个类来定义和设置参数,它是所有 Option 实例的容器.在 CLI 中,目前有两种方式来创建 Options,一种是通过构造函数,这是最普通也是最为大家所熟知的一种方式:另外一种方法是通过 Options 中定义的工厂方式

Apache Commons logging简介和使用

本章节内容主要来自网络和整理. Apache Commons Logging,又叫做JakartaCommons Logging (JCL),他提供的是一个日志(Log)接口(interface),同时兼顾轻量级和不依赖于具体的日志实现工具.它提供给中间件/日志工具开发者一个简单的日志操作抽象,允许程序开发人员使用不同的具体日志实现工具.用户被假定已熟悉某种日志实现工具的更高级别的细节.JCL提供的接口,对其它一些日志工具,包括Log4J, Avalon LogKit, and JDK等,进行了

Apache commons Proxy简介和动态代理、动态拦截器实现

Apache Commons Proxy 是Apache 的一个之项目,封装了 Java 对象代理的一些常用方法.又叫做 动态代理. 动态代理的作用非常大,在很多底层框架中都会用得到,比如struts,Spring等都用到了动态代理,它的原理很简单,就是将你要使用的类,重新生成一个子类或本类,这样框架就可以利用这个新生成的类做一些事情,比如在该类的方法前后加一些代码. 设想一下,不用修改任何已经编写好的代码,只要使用动态代理就可以灵活的加入一些东西,将来要是不喜欢了,不用也不会影响原来的代码.