org.xnap.util
Class FileHelper

java.lang.Object
  extended by org.xnap.util.FileHelper

public class FileHelper
extends java.lang.Object

Provides a set of static methods that help with file manipulation.


Constructor Summary
FileHelper()
           
 
Method Summary
static java.lang.String appendSeparator(java.lang.String path)
          Appens a file separator to path if it does not have a trailing one.
static void copy(java.io.File source, java.io.File dest)
          Copies source to dest.
static void copy(java.io.InputStream inStream, java.io.OutputStream outStream)
          Copies source to dest.
static java.io.File createIncompleteFile(java.lang.String filename)
          Creates and returns a file in the incomplete directory.
static java.io.File createUnique(java.io.File path, java.lang.String filename)
          Creates a unique file in by inserting digits into filename.
static java.io.File createUnique(java.lang.String filename)
          Creates a unique file by inserting digits into filename.
static java.lang.String directories(java.lang.String dirs)
           
static java.lang.String directory(java.lang.String dir)
           
static boolean exists(java.lang.String filename)
          Returns true, if filename exists; false, otherwise.
static java.lang.String extension(java.lang.String filename)
          Returns the lower case extension part of filename.
static java.lang.String getDownloadDir(java.lang.String filename)
          Returns the path of the download dir where filename should be downloaded to.
static java.lang.String getHomeDir()
          Returns the absolute path of the ~/.xnap/ directory.
static java.lang.String getHomeDir(java.lang.String subdir)
          Checks for existence of .xnap folder in the user's home directory and returns the absolute path with a file separator appended.
static java.net.URL getResource(java.lang.String filename)
           
static java.io.InputStream getResourceAsStream(java.lang.String filename)
           
static boolean move(java.io.File source, java.io.File dest)
          Moves a file.
static java.io.File moveUnique(java.io.File file, java.lang.String path)
          Moves file to path.
static java.io.File moveUnique(java.io.File file, java.lang.String path, java.lang.String filename)
          Moves file to path but renames filename if it already exists in the target path.
static java.lang.String name(java.lang.String filename)
          Returns the name part of filename.
static void readBinary(java.io.File file, java.util.Collection c)
          Reads all objects from file using serialization and adds them to c.
static java.lang.String[] readConfig(java.io.File file)
           
static java.lang.String[] readConfig(java.io.InputStream inStream)
          Reads a text file.
static java.lang.String readText(java.io.File file)
           
static java.lang.String readText(java.io.InputStream inStream)
          Reads a text file.
static void shorten(java.io.File file, long bytes)
          Shortens file by bytes bytes.
static java.lang.String toAscii(java.lang.String filename)
          Replaces all special characters in filename by '_'.
static java.lang.String toFilename(java.lang.String filename)
          Replaces some special characters in filename by '_'.
static java.lang.String uniqueName(java.lang.String filename)
          Creates unique filename.
static java.lang.String uniqueName(java.lang.String filename, java.lang.String infix)
           
static void writeBinary(java.io.File file, java.util.Collection c)
          Write all items in c to file using serialization.
static void writeProperties(java.io.File file, java.util.Properties props)
          Stores props in file.
static void writeText(java.io.File file, java.lang.String text)
          Writes a text file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileHelper

public FileHelper()
Method Detail

createIncompleteFile

public static java.io.File createIncompleteFile(java.lang.String filename)
                                         throws java.io.IOException
Creates and returns a file in the incomplete directory.

Throws:
java.io.IOException

createUnique

public static java.io.File createUnique(java.lang.String filename)
                                 throws java.io.IOException
Creates a unique file by inserting digits into filename.

Returns:
the created file
Throws:
java.io.IOException

createUnique

public static java.io.File createUnique(java.io.File path,
                                        java.lang.String filename)
                                 throws java.io.IOException
Creates a unique file in by inserting digits into filename.

Parameters:
path - the path of the file to create
filename - the name of the file to create
Returns:
the created file
Throws:
java.io.IOException

moveUnique

public static java.io.File moveUnique(java.io.File file,
                                      java.lang.String path,
                                      java.lang.String filename)
                               throws java.io.IOException
Moves file to path but renames filename if it already exists in the target path.

Parameters:
file - the file to move
path - the target path
filename - the new filename for file
Returns:
the moved file
Throws:
java.io.IOException

moveUnique

public static java.io.File moveUnique(java.io.File file,
                                      java.lang.String path)
                               throws java.io.IOException
Moves file to path.

Throws:
java.io.IOException
See Also:
moveUnique(File, String, String)

move

public static boolean move(java.io.File source,
                           java.io.File dest)
                    throws java.io.IOException
Moves a file. Tries a rename, if fails, copies file.

Throws:
java.io.IOException

copy

public static void copy(java.io.File source,
                        java.io.File dest)
                 throws java.io.IOException
Copies source to dest. If dest already exists it is overwritten.

Parameters:
source - the source file
dest - the destination file
Throws:
java.io.IOException

copy

public static void copy(java.io.InputStream inStream,
                        java.io.OutputStream outStream)
                 throws java.io.IOException
Copies source to dest. If dest already exists it is overwritten.

Parameters:
source - the source file
dest - the destination file
Throws:
java.io.IOException

extension

public static java.lang.String extension(java.lang.String filename)
Returns the lower case extension part of filename.

See Also:
name(String)

name

public static java.lang.String name(java.lang.String filename)
Returns the name part of filename.

See Also:
extension(String)

getDownloadDir

public static java.lang.String getDownloadDir(java.lang.String filename)
Returns the path of the download dir where filename should be downloaded to.


getResource

public static java.net.URL getResource(java.lang.String filename)

getResourceAsStream

public static java.io.InputStream getResourceAsStream(java.lang.String filename)

uniqueName

public static java.lang.String uniqueName(java.lang.String filename)
Creates unique filename.


uniqueName

public static java.lang.String uniqueName(java.lang.String filename,
                                          java.lang.String infix)

exists

public static boolean exists(java.lang.String filename)
Returns true, if filename exists; false, otherwise.


getHomeDir

public static final java.lang.String getHomeDir(java.lang.String subdir)
Checks for existence of .xnap folder in the user's home directory and returns the absolute path with a file separator appended.

Parameters:
subdir - a sub directory that is located in ~/.xnap/ or created if it does not exist
Returns:
empty string, if subdir could not be created; absolute path, otherwise

getHomeDir

public static final java.lang.String getHomeDir()
Returns the absolute path of the ~/.xnap/ directory.

See Also:
getHomeDir(String)

appendSeparator

public static java.lang.String appendSeparator(java.lang.String path)
Appens a file separator to path if it does not have a trailing one.


directory

public static java.lang.String directory(java.lang.String dir)

directories

public static java.lang.String directories(java.lang.String dirs)

shorten

public static void shorten(java.io.File file,
                           long bytes)
Shortens file by bytes bytes.


writeProperties

public static void writeProperties(java.io.File file,
                                   java.util.Properties props)
                            throws java.io.IOException
Stores props in file.

Throws:
java.io.IOException

readBinary

public static void readBinary(java.io.File file,
                              java.util.Collection c)
                       throws java.io.IOException
Reads all objects from file using serialization and adds them to c.

Throws:
java.io.IOException

readText

public static java.lang.String readText(java.io.File file)
                                 throws java.io.IOException
Throws:
java.io.IOException

readText

public static java.lang.String readText(java.io.InputStream inStream)
                                 throws java.io.IOException
Reads a text file.

Throws:
java.io.IOException

readConfig

public static java.lang.String[] readConfig(java.io.File file)
                                     throws java.io.IOException
Throws:
java.io.IOException

readConfig

public static java.lang.String[] readConfig(java.io.InputStream inStream)
                                     throws java.io.IOException
Reads a text file. Ignores all lines empty lines and lines that start with a '#' character.

Returns:
always a valid array, possibly with size 0
Throws:
java.io.IOException

toAscii

public static java.lang.String toAscii(java.lang.String filename)
Replaces all special characters in filename by '_'.


toFilename

public static java.lang.String toFilename(java.lang.String filename)
Replaces some special characters in filename by '_'.


writeBinary

public static void writeBinary(java.io.File file,
                               java.util.Collection c)
                        throws java.io.IOException
Write all items in c to file using serialization.

Throws:
java.io.IOException

writeText

public static void writeText(java.io.File file,
                             java.lang.String text)
                      throws java.io.IOException
Writes a text file.

Throws:
java.io.IOException


Copyright © 2001-2005 XNap Team. All Rights Reserved.