码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-12-22 16:18:27      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:java   apache   vfs   

         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 classShowProperties {

    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

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

标签:java   apache   vfs   

原文地址:http://blog.csdn.net/ffm83/article/details/42080325

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!