|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.openide.filesystems.FileUtil
Common utilities for handling files. This is a dummy class; all methods are static.
Method Summary | |
static void |
copy(InputStream is,
OutputStream os)
Copies stream of files. |
static void |
copyAttributes(FileObject source,
FileObject dest)
Copies attributes from one file to another. |
static FileObject |
copyFile(FileObject source,
FileObject destFolder,
String newName)
Copies file to the selected folder. |
static FileObject |
copyFile(FileObject source,
FileObject destFolder,
String newName,
String newExt)
Copies file to the selected folder. |
static FileObject |
createData(FileObject folder,
String name)
Creates a data file on given filesystem. |
static FileObject |
createFolder(FileObject folder,
String name)
Creates a folder on given filesystem. |
static FileSystem |
createMemoryFileSystem()
Factory method that creates an empty implementation of a filesystem that completely resides in a memory. |
static void |
extractJar(FileObject fo,
InputStream is)
Deprecated. Use of XML filesystem layers generally obsoletes this method. |
static FileObject |
findBrother(FileObject fo,
String ext)
Finds brother file with same base name but different extension. |
static String |
findFreeFileName(FileObject folder,
String name,
String ext)
Finds an unused file name similar to that requested in the same folder. |
static String |
findFreeFolderName(FileObject folder,
String name)
Finds an unused folder name similar to that requested in the same parent folder. |
static FileObject[] |
fromFile(File file)
Deprecated. Use toFileObject(java.io.File) instead. |
static FileObject |
getArchiveFile(FileObject fo)
Returns a FileObject representing an archive file containg the FileObject given by the parameter. |
static URL |
getArchiveFile(URL url)
Returns the URL of the archive file containing the file referred to by a jar -protocol URL. |
static FileObject |
getArchiveRoot(FileObject fo)
Returns a FileObject representing the root folder of an archive. |
static URL |
getArchiveRoot(URL url)
Returns a URL representing the root of an archive. |
static String |
getExtension(String fileName)
Gets the extension of a specified file name. |
static String |
getFileDisplayName(FileObject fo)
Get an appropriate display name for a file object. |
static String |
getMIMEType(FileObject fo)
Resolves MIME type. |
static String |
getMIMEType(String ext)
Deprecated. in favour of getMIMEType(FileObject) as MIME cannot
be generaly detected by a file object extension. |
static String |
getRelativePath(FileObject folder,
FileObject fo)
Gets a relative resource path between folder and fo. |
static boolean |
isArchiveFile(FileObject fo)
Tests if a file represents a JAR or ZIP archive. |
static boolean |
isArchiveFile(URL url)
Tests if a URL represents a JAR or ZIP archive. |
static boolean |
isParentOf(FileObject folder,
FileObject fo)
Recursively checks whether the file is underneath the folder. |
static FileObject |
moveFile(FileObject source,
FileObject destFolder,
String newName)
Moves file to the selected folder. |
static URLStreamHandler |
nbfsURLStreamHandler()
Construct a stream handler that handles the nbfs URL protocol
used for accessing file objects directly. |
static File |
normalizeFile(File file)
Normalize a file path to a clean form. |
static void |
preventFileChooserSymlinkTraversal(JFileChooser chooser,
File currentDirectory)
Make sure that a JFileChooser does not traverse symlinks on Unix. |
static void |
setMIMEType(String ext,
String mimeType)
Deprecated. You should instead use the more general MIMEResolver system. |
static File |
toFile(FileObject fo)
Finds appropriate java.io.File to FileObject if possible. |
static FileObject |
toFileObject(File file)
Converts a disk file to a matching file object. |
static FileChangeListener |
weakFileChangeListener(FileChangeListener l,
Object source)
Creates a weak implementation of FileChangeListener. |
static FileStatusListener |
weakFileStatusListener(FileStatusListener l,
Object source)
Creates a weak implementation of FileStatusListener. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Method Detail |
public static void copy(InputStream is, OutputStream os) throws IOException
Please be aware, that this method doesn't close any of passed streams.
is
- input streamos
- output stream
IOException
public static FileSystem createMemoryFileSystem()
public static FileObject copyFile(FileObject source, FileObject destFolder, String newName, String newExt) throws IOException
source
- source file objectdestFolder
- destination foldernewName
- file name (without extension) of destination filenewExt
- extension of destination file
IOException
- if destFolder
is not a folder or does not exist; the destination file already exists; or
another critical error occurs during copyingpublic static FileObject copyFile(FileObject source, FileObject destFolder, String newName) throws IOException
source
- source file objectdestFolder
- destination foldernewName
- file name (without extension) of destination file
IOException
- if destFolder
is not a folder or does not exist; the destination file already exists; or
another critical error occurs during copyingpublic static FileObject moveFile(FileObject source, FileObject destFolder, String newName) throws IOException
source
- source file objectdestFolder
- destination foldernewName
- file name (without extension) of destination file
IOException
- if either the copy
or delete
failedpublic static FileObject createFolder(FileObject folder, String name) throws IOException
folder
- where the new folder will be placed inname
- name of the new folder
IOException
- if the creation failspublic static FileObject createData(FileObject folder, String name) throws IOException
folder
- to begin with creation atname
- name of data file as a resource
IOException
- if the creation failspublic static File toFile(FileObject fo)
toFileObject(java.io.File)
.
fo
- FileObject whose coresponding File will be looked for
public static FileObject toFileObject(File file) throws IllegalArgumentException
toFile(org.openide.filesystems.FileObject)
.
If you are running with the MasterFS module enabled, that will guarantee that this method never returns null for a file which exists on disk.
file
- a disk file (may or may not exist)
URLMapper
available to convert it
IllegalArgumentException
- if the file is not normalized
public static FileObject[] fromFile(File file)
toFileObject(java.io.File)
instead.
file
- File whose coresponding FileObjects will be looked for.
The file has to be "normalized" otherwise IllegalArgumentException is thrown.
See normalizeFile(java.io.File)
for how to do that.
public static void copyAttributes(FileObject source, FileObject dest) throws IOException
source
- source file objectdest
- destination file object
IOException
- if the copying failedpublic static void extractJar(FileObject fo, InputStream is) throws IOException
META-INF/
directories are skipped over.
fo
- file object of destination folderis
- input stream of jar file
IOException
- if the extraction failspublic static String getExtension(String fileName)
fileName
- name of the file
""
if it had none)public static String findFreeFileName(FileObject folder, String name, String ext)
isVirtual
.
Otherwise, the first available name of the form basename_nnn.ext
(counting from one) is used.
Caution: this method does not lock the parent folder
to prevent race conditions: i.e. it is possible (though unlikely)
that the resulting name will have been created by another thread
just as you were about to create the file yourself (if you are,
in fact, intending to create it just after this call). Since you
cannot currently lock a folder against child creation actions,
the safe approach is to use a loop in which a free name is
retrieved; an attempt is made to create
that file; and upon an IOException
during
creation, retry the loop up to a few times before giving up.
folder
- parent foldername
- preferred base name of fileext
- extension to use
public static String findFreeFolderName(FileObject folder, String name)
See caveat for findFreeFileName
.
folder
- parent foldername
- preferred folder name
findFreeFileName
public static String getRelativePath(FileObject folder, FileObject fo)
folder
- root of filesystem or any other folder in folders hierarchyfo
- arbitrary FileObject in folder's tree (including folder itself)
null
if fo is not in
folder's tree.isParentOf(org.openide.filesystems.FileObject, org.openide.filesystems.FileObject)
public static FileObject findBrother(FileObject fo, String ext)
fo
- the file to find the brother for or null
ext
- extension for the brother file
null
if the brother file does not exist or the original file was null
public static String getMIMEType(String ext)
getMIMEType(FileObject)
as MIME cannot
be generaly detected by a file object extension.
ext
- the extension: "jar"
, "zip"
, etc.
null
if the extension is unrecognizedpublic static String getMIMEType(FileObject fo)
fo
- whose MIME type should be recognized
null
if the FileObject is unrecognizedpublic static void setMIMEType(String ext, String mimeType)
MIMEResolver
system.
ext
- the file extension (should be lowercase unless you specifically care about case)mimeType
- the new MIME type
IllegalArgumentException
- if this extension was already registered with a different MIME typegetMIMEType(java.lang.String)
public static URLStreamHandler nbfsURLStreamHandler()
nbfs
URL protocol
used for accessing file objects directly.
This method is not intended for module use; only the core
should need to call it.
Modules probably need only use URLMapper
to create and decode such
URLs.
public static boolean isParentOf(FileObject folder, FileObject fo)
FileObject
of the file recursively until the folder is found
or the root of the filesystem is reached.
Warning: this method will return false in the case that
folder == fo
.
folder
- the root of folders hierarchy to search info
- the file to search for
true
, if fo
lies somewhere underneath the folder
,
false
otherwisepublic static FileChangeListener weakFileChangeListener(FileChangeListener l, Object source)
l
- the listener to delegate tosource
- the source that the listener should detach from when
listener l
is freed, can be null
l
.public static FileStatusListener weakFileStatusListener(FileStatusListener l, Object source)
l
- the listener to delegate tosource
- the source that the listener should detach from when
listener l
is freed, can be null
l
.public static String getFileDisplayName(FileObject fo)
FileObject
isn't root FileObject.isRoot()
.
fo
- a file object
public static File normalizeFile(File file)
File.getCanonicalFile()
this method will not traverse symbolic links on Unix.
file
- file to normalize
public static FileObject getArchiveRoot(FileObject fo)
isArchiveFile(FileObject)
to determine
if the file object refers to an archive file.
fo
- a ZIP- (or JAR-) format archive file
public static URL getArchiveRoot(URL url)
isArchiveFile(URL)
to determine if the URL
refers to an archive file.
url
- of a ZIP- (or JAR-) format archive file
jar
-protocol URL of the root of the archivepublic static FileObject getArchiveFile(FileObject fo)
fo
- a file in a JAR filesystem
fo
is not an archive entrypublic static URL getArchiveFile(URL url)
jar
-protocol URL.
Remember that any path within the archive is discarded
so you may need to check for non-root entries.
url
- a URL
jar
-protocol URL containing !/
public static boolean isArchiveFile(FileObject fo)
fo
- the file to be tested
public static boolean isArchiveFile(URL url)
url
- a URL to a file
public static void preventFileChooserSymlinkTraversal(JFileChooser chooser, File currentDirectory)
chooser
- a file choosercurrentDirectory
- if not null, a file to set as the current directory
using JFileChooser.setCurrentDirectory(java.io.File)
without canonicalizing
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |