com.marringtons.file
Class Output

java.lang.Object
  extended bycom.marringtons.file.Output

public class Output
extends Object

Helper class for file output - find output files in the directory system, return absolute paths, make unique file and retrieve output streams or writers.

Author:
Paul Marrington

Field Summary
static String basePath
          Directory to write files to (if not absolute).
 
Method Summary
static File getFile(String name)
          Return a file object for an output file.
static String makeAbsolute(String name)
          Return an absolute path to a file for output - either from the original name or in the output directory.
static String makeUnique(String name, String type)
          Create a unique file name - easily read to include date and time.
static void set(String dir)
          Set the output directory - normally from the application name.
static OutputStream stream(String name, boolean append)
          Given a file name, create or append to a file.
static Writer writer(String name, boolean append)
          Given a file name, create or append to a file.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

basePath

public static String basePath
Directory to write files to (if not absolute). Defaults to the current working directory when the program starts. If the program registers itself with Application.start the output directory is moved to "userDir/appname" or "homeDir/appname" if one of these directories exist - otherwise it is left in userDir. See Application.start().

Method Detail

set

public static void set(String dir)
Set the output directory - normally from the application name. Called by Application.start(); Set to "userDir/appname" or "homeDir/appname" if one of these directories exist - otherwise it is created as "userDir/appname". Use ~/dir to be relative to home directory

Parameters:
dir - directory to use to files being created.

stream

public static OutputStream stream(String name,
                                  boolean append)
                           throws FileNotFoundException
Given a file name, create or append to a file. Resulting directory relies on path.

Parameters:
name - of file
append - true to add to file, false to create new file (or clear old)
Returns:
buffered output stream to write to
Throws:
FileNotFoundException

writer

public static Writer writer(String name,
                            boolean append)
                     throws IOException
Given a file name, create or append to a file. Resulting directory relies on path.

Parameters:
name - of file
append - true to add to file, false to create new file (or clear old)
Returns:
buffered output stream to write to
Throws:
IOException

makeAbsolute

public static String makeAbsolute(String name)
Return an absolute path to a file for output - either from the original name or in the output directory. ~session~ is expanded to the thread name. This means it can return paths with the name if the current session in them. This allows you to save session dependant data.

Parameters:
name - to be turned into an absolute path
Returns:
absolute name given the relative file name.

makeUnique

public static String makeUnique(String name,
                                String type)
Create a unique file name - easily read to include date and time. Only works for one instance creating files in the same directory.

Parameters:
name - to describe the file
type - file type (extension - as in txt).
Returns:
unique string including name, date, time and a unique run ID.

getFile

public static File getFile(String name)
Return a file object for an output file. Relative paths will be from the output directory

Parameters:
name - of file to build File object instance for.
Returns:
File object describing this named file.


Copyright © 2005 Paul Marrington http://library.marringtons.com