org.openide.filesystems
Class FileUtil

java.lang.Object
  |
  +--org.openide.filesystems.FileUtil

public final class FileUtil
extends Object

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 file system.
static FileObject createFolder(FileObject folder, String name)
          Creates a folder on given file system.
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)
          Finds appropriate FileObjects to java.io.File if possible.
static String getExtension(String fileName)
          Gets the extension of a specified file name.
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 FileObject moveFile(FileObject source, FileObject destFolder, String newName)
          Moves file to the selected folder.
static void setMIMEType(String ext, String mimeType)
          Register MIME type for a new extension.
static File toFile(FileObject fo)
          Finds appropriate java.io.File to FileObject if possible.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

copy

public static void copy(InputStream is,
                        OutputStream os)
                 throws IOException
Copies stream of files.
Parameters:
is - input stream
os - output stream

copyFile

public static FileObject copyFile(FileObject source,
                                  FileObject destFolder,
                                  String newName,
                                  String newExt)
                           throws IOException
Copies file to the selected folder. This implementation simply copies the file by stream content.
Parameters:
source - source file object
destFolder - destination folder
newName - file name (without extension) of destination file
newExt - extension of destination file
Returns:
the created file object in the destination folder
Throws:
IOException - if destFolder is not a folder or does not exist; the destination file already exists; or another critical error occurs during copying

copyFile

public static FileObject copyFile(FileObject source,
                                  FileObject destFolder,
                                  String newName)
                           throws IOException
Copies file to the selected folder. This implementation simply copies the file by stream content. Uses the extension of the source file.
Parameters:
source - source file object
destFolder - destination folder
newName - file name (without extension) of destination file
Returns:
the created file object in the destination folder
Throws:
IOException - if destFolder is not a folder or does not exist; the destination file already exists; or another critical error occurs during copying

moveFile

public static FileObject moveFile(FileObject source,
                                  FileObject destFolder,
                                  String newName)
                           throws IOException
Moves file to the selected folder. This implementation uses a copy-and-delete mechanism, and automatically uses the necessary lock.
Parameters:
source - source file object
destFolder - destination folder
newName - file name (without extension) of destination file
Returns:
new file object
Throws:
IOException - if either the copy or delete failed

createFolder

public static FileObject createFolder(FileObject folder,
                                      String name)
                               throws IOException
Creates a folder on given file system. The name of folder can be composed as resource name (e. g. org/netbeans/myfolder) and the method scans which of folders has already been created and which not. If successful the caller can be sure that the folder is there and receives a reference to it.
Parameters:
folder - to begin with creation at
name - name of folder as a resource
Returns:
the folder for given name
Throws:
IOException - if the creation fails

createData

public static FileObject createData(FileObject folder,
                                    String name)
                             throws IOException
Creates a data file on given file system. The name of data file can be composed as resource name (e. g. org/netbeans/myfolder/mydata ) and the method scans which of folders has already been created and which not.
Parameters:
folder - to begin with creation at
name - name of data file as a resource
Returns:
the data file for given name
Throws:
IOException - if the creation fails

toFile

public static File toFile(FileObject fo)
Finds appropriate java.io.File to FileObject if possible. If not possible then null is returned.
Parameters:
fo - FileObject whose coresponding File will be looked for
Returns:
java.io.File or null if no corresponding File exists.
Since:
1.29

fromFile

public static FileObject[] fromFile(File file)
Finds appropriate FileObjects to java.io.File if possible. If not possible then empty array is returned. More FileObjects may correspond to one java.io.File that`s why array is returned.
Parameters:
fo - File whose coresponding FileObjects will be looked for
Returns:
corresponding FileObjects or empty array if no corresponding FileObject exists.
Since:
1.29

copyAttributes

public static void copyAttributes(FileObject source,
                                  FileObject dest)
                           throws IOException
Copies attributes from one file to another. Note: several special attributes will not be copied, as they should semantically be transient. These include attributes used by the template wizard (but not the template atttribute itself).
Parameters:
source - source file object
dest - destination file object
Throws:
IOException - if the copying failed

extractJar

public static void extractJar(FileObject fo,
                              InputStream is)
                       throws IOException
Deprecated. Use of XML filesystem layers generally obsoletes this method.

Extract jar file into folder represented by file object. If the JAR contains files with name filesystem.attributes, it is assumed that these files has been created by DefaultAttributes implementation and the content of these files is treated as attributes and added to extracted files.

META-INF/ directories are skipped over.

Parameters:
fo - file object of destination folder
is - input stream of jar file
Throws:
IOException - if the extraction fails

getExtension

public static String getExtension(String fileName)
Gets the extension of a specified file name. The extension is everything after the last dot.
Parameters:
fileName - name of the file
Returns:
extension of the file (or "" if it had none)

findFreeFileName

public static String findFreeFileName(FileObject folder,
                                      String name,
                                      String ext)
Finds an unused file name similar to that requested in the same folder. The specified file name is used if that does not yet exist. 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.

Parameters:
df - parent folder
name - preferred base name of file
ext - extension to use
Returns:
a free file name

findFreeFolderName

public static String findFreeFolderName(FileObject folder,
                                        String name)
Finds an unused folder name similar to that requested in the same parent folder.

See caveat for findFreeFileName.

Parameters:
df - parent folder
name - preferred folder name
Returns:
a free folder name
See Also:
findFreeFileName

findBrother

public static FileObject findBrother(FileObject fo,
                                     String ext)
Finds brother file with same base name but different extension.
Parameters:
fo - the file to find the brother for or null
ext - extension for the brother file
Returns:
a brother file (with the requested extension and the same parent folder as the original) or null if the brother file does not exist or the original file was null

getMIMEType

public static String getMIMEType(String ext)
Deprecated. in favour of getMIMEType(FileObject) as MIME cannot be generaly detected by a file object extension.

Obtain MIME type for a well-known extension. If there is a case-sensitive match, that is used, else will fall back to a case-insensitive match.
Parameters:
ext - the extension: "jar", "zip", etc.
Returns:
the MIME type for the extension, or null if the extension is unrecognized

getMIMEType

public static String getMIMEType(FileObject fo)
Resolves MIME type. Registered resolvers are invoked and used to achieve this goal. Resolvers must subclass MIMEResolver. If resolvers don`t recognize MIME type then MIME type is obtained for a well-known extension.
Parameters:
fo - whose MIME type should be recognized
Returns:
the MIME type for the FileObject, or null if the FileObject is unrecognized

setMIMEType

public static void setMIMEType(String ext,
                               String mimeType)
Register MIME type for a new extension. Note that you may register a case-sensitive extension if that is relevant (for example *.C for C++) but if you register a lowercase extension it will by default apply to uppercase extensions too (for use on Windows or generally for situations where filenames become accidentally upcased).
Parameters:
ext - the file extension (should be lowercase unless you specifically care about case)
mimeType - the new MIME type
Throws:
IllegalArgumentException - if this extension was already registered with a different MIME type
See Also:
getMIMEType(java.lang.String)


Built on December 12 2001.  |  Portions Copyright 1997-2001 Sun Microsystems, Inc. All rights reserved.