Apache Commons VFS Introduction and ShowProperties Source code example

Source: Internet
Author: User
Tags dateformat

Apache Commons VFS is also known as Apache Commons Virtual FileSystem. is a powerful set of access to a variety of resources, the JAR package has been completely new refactoring, the latest version is 2.2.

If we need to get some information of different format files in our normal work, such as file size, path, file last change time, etc., or we need to do some regular operation on the file, such as deleting files, copying files and so on, then Apache The VFS (Virtual File System) in Commons is an open source system that we can consider.

According to the VFS website, it currently supports the following file formats:

FTP, Local Files, HTTP and HTTPS, SFTP, temporary Files, Zip, Jar and Tar (uncompressed, tgz or tbz2), gzip and bzip2, res , Ram, mime.

Its official website is: http://commons.apache.org/vfs/

when we need to work on the file, we should first think of VFS .

VFS2 There are some self-brought Example , study under:

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;

/**

 * through VFS get some information about a file

 * Use 2.0 Version Implementation

 * @author example, modification: Fan Fangming

*/

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 ();

}

}

}

The results of the operation are as follows:

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 Introduction and ShowProperties Source code example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.