org.xnap.commons.util
Class FileHelper

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

public class FileHelper
extends Object

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


Constructor Summary
FileHelper()
           
 
Method Summary
static String appendSeparator(String path)
          Appens a file separator to path if it does not have a trailing one.
static void copy(File source, File dest)
           
static void copy(File source, File dest, ProgressMonitor monitor)
          Copies source to dest.
static void copy(InputStream inStream, OutputStream outStream)
           
static void copy(InputStream inStream, OutputStream outStream, ProgressMonitor monitor)
          Copies source to dest.
static File createUnique(File parent, String child)
          Creates a unique file in by inserting digits into filename.
static File createUnique(String pathname)
          Creates a unique file by inserting digits into pathname.
static boolean exists(String pathname)
          Returns true, if the file denoting pathname exists, false otherwise.
static String extension(String filename)
          Returns the lower case extension part of filename.
static String getHomeDir(String appname)
          Returns the absolute path of the applications settings directory.
static String getHomeDir(String appname, 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 boolean move(File source, File dest)
          Moves a file.
static File moveUnique(File file, String path)
          Moves file to path.
static File moveUnique(File file, String path, String filename)
          Moves file to path but renames filename if it already exists in the target path.
static String name(String filename)
          Returns the name part of filename without its extension.
static void readBinary(File file, Collection<Object> c)
          Reads all objects from file using serialization and adds them to c.
static String[] readConfig(File file)
          TODO provide a symmetrical write function?
static String[] readConfig(InputStream inStream)
          Reads a text file.
static String readText(File file)
           
static String readText(InputStream inStream)
          Reads a text file.
static void shorten(File file, long bytes)
          Shortens file by bytes bytes.
static String uniqueName(String filename)
          Creates unique filename.
static String uniqueName(String filename, String infix)
           
static void writeBinary(File file, Collection c)
          Write all items in c to file using serialization.
static void writeProperties(File file, Properties props)
          Stores props in file.
static void writeText(File file, 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

createUnique

public static File createUnique(String pathname)
                         throws IOException
Creates a unique file by inserting digits into pathname.

Returns:
the created file
Throws:
IOException

createUnique

public static File createUnique(File parent,
                                String child)
                         throws IOException
Creates a unique file in by inserting digits into filename.

Parameters:
child - the name of the file to create
parent - the path of the file to create
Returns:
the created file
Throws:
IOException

moveUnique

public static File moveUnique(File file,
                              String path,
                              String filename)
                       throws 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:
IOException

moveUnique

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

Throws:
IOException
See Also:
moveUnique(File, String, String)

move

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

Throws:
IOException

copy

public static void copy(File source,
                        File dest)
                 throws IOException
Throws:
IOException

copy

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

Parameters:
source - the source file
dest - the destination file
Throws:
IOException

copy

public static void copy(InputStream inStream,
                        OutputStream outStream)
                 throws IOException
Throws:
IOException

copy

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

Parameters:
inStream - the source data
outStream - the destination data
Throws:
IOException

extension

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

See Also:
name(String)

name

public static String name(String filename)
Returns the name part of filename without its extension.

See Also:
extension(String)

uniqueName

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


uniqueName

public static String uniqueName(String filename,
                                String infix)

exists

public static boolean exists(String pathname)
Returns true, if the file denoting pathname exists, false otherwise.


getHomeDir

public static final String getHomeDir(String appname,
                                      String subdir)
                               throws IOException
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 the applications settings directory or created if it does not exist
Returns:
empty string, if subdir could not be created; absolute path, otherwise
Throws:
IOException

getHomeDir

public static final String getHomeDir(String appname)
                               throws IOException
Returns the absolute path of the applications settings directory.

Throws:
IOException
See Also:
getHomeDir(String)

appendSeparator

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


shorten

public static void shorten(File file,
                           long bytes)
                    throws IOException
Shortens file by bytes bytes.

Throws:
IOException

writeProperties

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

Throws:
IOException

readBinary

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

Throws:
IOException

readText

public static String readText(File file)
                       throws IOException
Throws:
IOException

readText

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

Throws:
IOException

readConfig

public static String[] readConfig(File file)
                           throws IOException
TODO provide a symmetrical write function?

Parameters:
file -
Throws:
IOException

readConfig

public static String[] readConfig(InputStream inStream)
                           throws 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:
IOException

writeBinary

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

Throws:
IOException

writeText

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

Throws:
IOException


Copyright © 2004-2007 XNap Commons Team. All Rights Reserved.