Java vfs to file

Java vfs to file

Represents a file, and is used to access the content and structure of the file. Files are arranged in a hierarchy. Each hierarchy forms a file system. A file system represents things like a local OS file system, a windows share, an HTTP server, or the contents of a Zip file. There are two types of files: Folders, which contain other files, and normal files, which contain data, or content. A folder may not have any content, and a normal file cannot contain other files.

File Naming

Reading and Writing a File

Reading and writing a file, and all other operations on the file’s content, is done using the FileContent object returned by getContent() .

Creating and Deleting a File

A file is created using either createFolder() , createFile() , or by writing to the file using one of the FileContent methods. A file is deleted using delete() . Recursive deletion can be done using delete(FileSelector) .

Finding Files

  • findFiles(org.apache.commons.vfs2.FileSelector) to find a set of matching descendants in in the same file system.
  • getChildren() and getChild(java.lang.String) to find the children of this file.
  • getParent() to find the folder containing this file.
  • getFileSystem() to find another file in the same file system.
  • resolveFile(java.lang.String) to find another file relative to this file.

To find files in another file system, use a FileSystemManager .

Iterating Files

You can iterate over a FileObject using the Java «foreach» statement, which provides all descendants of a File Object.

Sorting Files

Field Summary

Method Summary

Methods inherited from interface java.lang.Comparable

Methods inherited from interface java.lang.Iterable

Field Detail

EMPTY_ARRAY

Method Detail

canRenameTo

close

Closes this file, and its content. This method is a hint to the implementation that it can release any resources associated with the file. The file object can continue to be used after this method is called.

copyFrom

void copyFrom(FileObject srcFile, FileSelector selector) throws FileSystemException

Copies another file, and all its descendants, to this file. If this file does not exist, it is created. Its parent folder is also created, if necessary. If this file does exist, it is deleted first. This method is not transactional. If it fails and throws an exception, this file will potentially only be partially copied.

Читайте также:  Java proxy get object

createFile

Creates this file, if it does not exist. Also creates any ancestor folders which do not exist. This method does nothing if the file already exists and is a file.

createFolder

Creates this folder, if it does not exist. Also creates any ancestor folders which do not exist. This method does nothing if the folder already exists.

delete

Deletes this file. Does nothing if this file does not exist of if it is a folder that has children. Does not delete any descendants of this file, use delete(FileSelector) or deleteAll() for that.

delete

int delete(FileSelector selector) throws FileSystemException

Deletes all descendants of this file that match a selector. Does nothing if this file does not exist. This method is not transactional. If it fails and throws an exception, this file will potentially only be partially deleted.

Источник

Java vfs to file

ApacheCon

Supported File Systems

Commons VFS directly supports the following file systems with the listed capabilities:

File System Directory Contents Authentication Read Write Create/Delete Random Version Rename
BZIP2 No No Yes Yes No No No No
File No No Yes Yes Yes Read/Write No Yes
FTP No Yes Yes Yes Yes Read No Yes
FTPS No Yes Yes Yes Yes Read No Yes
GZIP No No Yes Yes No No No No
HDFS Yes No Yes No No Read No No
HTTP Yes Yes Yes No No Read No No
HTTPS Yes Yes Yes No No Read No No
Jar No No Yes No No No No No
RAM No No Yes Yes Yes Read/Write No Yes
RES No No Yes Yes Yes Read/Write No Yes
SFTP No Yes Yes Yes Yes Read No Yes
Tar No No Yes No No No No No
Temp No No Yes Yes Yes Read/Write No Yes
WebDAV Yes Yes Yes Yes Yes Read/Write Yes Yes
Zip No No Yes No No No No No

Things from the sandbox

The following file systems are in development:

File System Directory Contents Authentication Read Write Create/Delete Random Version Rename
CIFS No Yes Yes Yes Yes Read/Write No Yes
mime No No Yes No No No No No

Naming

All filenames are treated as URIs. One of the consequences of this is you have to encode the ‘%’ character using %25 .
Depending on the filesystem additional characters are encoded if needed. This is done automatically, but might be reflected in the filename.

Читайте также:  Python array to end

Many file systems accept a userid and password as part of the url. However, storing a password in clear text in a file is usually unacceptable. To help with that Commons VFS provides a mechanism to encrypt the password. It should be noted though, that this is not completely secure since the password needs to be unencrypted before Commons VFS can use it.

To create an encrypted password do:

java -cp commons-vfs-2.0.jar org.apache.commons.vfs2.util.EncryptUtil encrypt mypassword

where mypassword is the password you want to encrypt. The result of this will be a single line of output containing uppercase hex characters. For example,

java -cp commons-vfs-2.0.jar org.apache.commons.vfs2.util.EncryptUtil encrypt WontUBee9 D7B82198B272F5C93790FEB38A73C7B8

Then cut the output returned and paste it into the URL as:

VFS treats a password enclosed in <> as being encrypted and will decrypt the password before using it.

Local Files

Provides access to the files on the local physical file system.

Where absolute-path is a valid absolute file name for the local platform. UNC names are supported under Windows.

  • file:///home/someuser/somedir
  • file:///C:/Documents and Settings
  • file://///somehost/someshare/afile.txt
  • /home/someuser/somedir
  • c:\program files\some dir
  • c:/program files/some dir

Zip, Jar and Tar

Provides read-only access to the contents of Zip, Jar and Tar files.

zip:// arch-file-uri[! absolute-path]

jar:// arch-file-uri[! absolute-path]

tar:// arch-file-uri[! absolute-path]

tgz:// arch-file-uri[! absolute-path]

tbz2:// arch-file-uri[! absolute-path]

Where arch-file-uri refers to a file of any supported type, including other zip files. Note: if you would like to use the ! as normal character it must be escaped using %21 .
tgz and tbz2 are convenience for tar:gz and tar:bz2 .

  • jar. /lib/classes.jar!/META-INF/manifest.mf
  • zip:http://somehost/downloads/somefile.zip
  • jar:zip:outer.zip!/nested.jar!/somedir
  • jar:zip:outer.zip!/nested.jar!/some%21dir
  • tar:gz:http://anyhost/dir/mytar.tar.gz!/mytar.tar!/path/in/tar/README.txt
  • tgz:file://anyhost/dir/mytar.tgz!/somepath/somefile

gzip and bzip2

Provides read-only access to the contents of gzip and bzip2 files.

gz:// compressed-file-uri

bz2:// compressed-file-uri

Where compressed-file-uri refers to a file of any supported type. There is no need to add a ! part to the URI if you read the content of the file you always will get the uncompressed version.

HDFS

Provides (read-only) access to files in an Apache Hadoop File System (HDFS). On Windows the integration test is disabled by default, as it requires binaries.

hdfs:// hostname[: port][ absolute-path]

HTTP and HTTPS

Provides access to files on an HTTP server.

http://[ username[: password]@] hostname[: port][ absolute-path]

https://[ username[: password]@] hostname[: port][ absolute-path]

  • proxyHost The proxy host to connect through.
  • proxyPort The proxy port to use.
  • proxyScheme The proxy scheme (http/https) to use.
  • cookies An array of Cookies to add to the request.
  • maxConnectionsPerHost The maximum number of connections allowed to a specific host and port. The default is 5.
  • maxTotalConnections The maximum number of connections allowed to all hosts. The default is 50.
  • keystoreFile The keystore file for SSL connections.
  • keystorePass The keystore password.
  • keystoreType The keystore type.
Читайте также:  Java set file date

WebDAV

Provides access to files on a WebDAV server through the modules commons-vfs2-jackrabbit1 and commons-vfs2-jackrabbit2 .

webdav://[ username[: password]@] hostname[: port][ absolute-path]

  • versioning true if versioning should be enabled
  • creatorName the user name to be identified with changes to a file. If not set the user name used to authenticate will be used.

FTP

Provides access to the files on an FTP server.

ftp://[ username[: password]@] hostname[: port][ relative-path]

By default, the path is relative to the user’s home directory. This can be changed with:

FTPS

Provides access to the files on an FTP server over SSL.

ftps://[ username[: password]@] hostname[: port][ absolute-path]

SFTP

Provides access to the files on an SFTP server (that is, an SSH or SCP server).

sftp://[ username[: password]@] hostname[: port][ relative-path]

By default, the path is relative to the user’s home directory. This can be changed with:

CIFS

The CIFS (sandbox) filesystem provides access to a CIFS server, such as a Samba server, or a Windows share.

smb://[ username[: password]@] hostname[: port][ absolute-path]

Temporary Files

Provides access to a temporary file system, or scratchpad, that is deleted when Commons VFS shuts down. The temporary file system is backed by a local file system.

Resource

This is not really a filesystem, it just tries to lookup a resource using javas ClassLoader.getResource() and creates a VFS url for further processing.

RAM

A filesystem which stores all the data in memory (one byte array for each file content).

MIME

This (sandbox) filesystem can read mails and its attachements like archives.
If a part in the parsed mail has no name, a dummy name will be generated. The dummy name is: _body_part_X where X will be replaced by the part number.

mime:// mime-file-uri[! absolute-path]

  • mime:file:///your/path/mail/anymail.mime!/
  • mime:file:///your/path/mail/anymail.mime!/filename.pdf
  • mime:file:///your/path/mail/anymail.mime!/_body_part_0

Copyright © 2002-2021 The Apache Software Foundation. All Rights Reserved.

Apache Commons, Apache Commons VFS Project, Apache, the Apache feather logo, and the Apache Commons project logos are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.

Источник

Оцените статью