com.cleancode.util
Class FilePlus

java.lang.Object
  extended by com.cleancode.util.FilePlus

public final class FilePlus
extends Object

Convenience methods for file handling.

Since:
CleanCode 0.9
Version:
$Revision: 9 $
Author:
Michael Sorens

Field Summary
static String VERSION
          Current version of this class.
 
Method Summary
static String getPath(String fname)
          Returns just the path portion of a file name.
static boolean newer(File srcfile, File destfile)
          Determines if a source file, or any file in a source directory, is newer than a destination file.
static boolean newer(File srcfile, File destfile, FilenameFilter filter)
          Determines if a source file, or any file in a source directory, is newer than a destination file.
static boolean newer(String srcfileName, File destfile, FilenameFilter filter)
          Determines if a file or directory of the specified name, is newer than a destination file.
static String readFile(BufferedReader reader)
          Returns the contents of the BufferedReader as a String.
static String readFile(InputStream stream)
          Returns the contents of the InputStream as a String.
static String readFile(String fname)
          Returns the contents of the named file as a String.
static String[] readFileAsArray(BufferedReader reader)
          Returns the contents of the BufferedReader as a string array.
static String[] readFileAsArray(InputStream stream)
          Returns the contents of the InputStream as a string array.
static String[] readFileAsArray(String fname)
          Returns the contents of the named file as a String array.
static String[] readFileAsArrayColumn(String fname, int column)
          Returns the contents of a single column in a field-delimited file as a String array.
static String[] readFileAsArrayColumn(String fname, String[][] filterList, int column)
          Returns the contents of a single column in a field-delimited file as a String array, filtering the input as it is read.
static String[] readFileAsArrayColumn(String fname, String[][] filterList, int column, String fieldSep)
          Returns the contents of a single column in a field-delimited file as a String array, filtering the input as it is read.
static boolean writeFile(String fname, String text)
          Write specified text to a file, overwriting the original, if any.
static boolean writeFile(String fname, String text, String backupExt)
          Write specified text to a file, backing up the original.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERSION

public static final String VERSION
Current version of this class.

Method Detail

readFile

public static String readFile(String fname)
                       throws IOException
Returns the contents of the named file as a String.

Parameters:
fname - name of the file to read
Returns:
contents of file as a String
Throws:
IOException - if any problems reading the file.

readFile

public static String readFile(InputStream stream)
                       throws IOException
Returns the contents of the InputStream as a String.

Parameters:
stream - input stream to read
Returns:
contents of file as a String
Throws:
IOException - if any problems reading the file.

readFile

public static String readFile(BufferedReader reader)
                       throws IOException
Returns the contents of the BufferedReader as a String.

Parameters:
reader - BufferedReader to read
Returns:
contents of file as a String
Throws:
IOException - if any problems reading the file.

readFileAsArray

public static String[] readFileAsArray(String fname)
                                throws IOException
Returns the contents of the named file as a String array.

Parameters:
fname - name of the file to read
Returns:
contents of file as an array
Throws:
IOException - if any problems reading the file.

readFileAsArray

public static String[] readFileAsArray(InputStream stream)
                                throws IOException
Returns the contents of the InputStream as a string array.

Parameters:
stream - input stream to read
Returns:
contents of file as an array
Throws:
IOException - if any problems reading the file.

readFileAsArray

public static String[] readFileAsArray(BufferedReader reader)
                                throws IOException
Returns the contents of the BufferedReader as a string array.

Parameters:
reader - BufferedReader to read
Returns:
contents of file as an array
Throws:
IOException - if any problems reading the file.

readFileAsArrayColumn

public static String[] readFileAsArrayColumn(String fname,
                                             int column)
Returns the contents of a single column in a field-delimited file as a String array.

Parameters:
fname - name of the file to read
column - column number (zero-based) to read from file
Returns:
contents of specified column in field-delimited file as an array

readFileAsArrayColumn

public static String[] readFileAsArrayColumn(String fname,
                                             String[][] filterList,
                                             int column)
Returns the contents of a single column in a field-delimited file as a String array, filtering the input as it is read. The filterList contains 2-item sub-lists, where each sub-list contains a regular expression to match, and a replacement expression once matched.

Parameters:
fname - name of the file to read
filterList - list of search/replace terms to filter while reading
column - column number (zero-based) to read from file
Returns:
contents of specified column in field-delimited file as an array
See Also:
readFileAsArrayColumn(String, String[][], int, String)

readFileAsArrayColumn

public static String[] readFileAsArrayColumn(String fname,
                                             String[][] filterList,
                                             int column,
                                             String fieldSep)
Returns the contents of a single column in a field-delimited file as a String array, filtering the input as it is read. The filterList contains 2-item sub-lists, where each sub-list contains a regular expression to match, and a replacement expression once matched. One application of this is to remove sensitive data. Another is for comparing files where, for example, datestamps in the file will always differ, but are not significant. Say for example there is a line "start time: Sat Dec 25 2:15pm" where the actual date and time will vary. Specifying a sublist of { "(start time:).*", "$1" } as one element in filterList will remove the varying part of the text.

Parameters:
fname - name of the file to read
filterList - list of search/replace terms to filter while reading
column - column number (zero-based) to read from file
fieldSep - field separator regular expression used in file
Returns:
contents of specified column in field-delimited file as an array

writeFile

public static boolean writeFile(String fname,
                                String text,
                                String backupExt)
Write specified text to a file, backing up the original. If the backup extension is specified, renames the original file appending that extension, before writing.

Parameters:
fname - file name
text - text to write
backupExt - extension for backup copy of the file
Returns:
boolean indicating whether write was successful.

writeFile

public static boolean writeFile(String fname,
                                String text)
Write specified text to a file, overwriting the original, if any.

Parameters:
fname - file name
text - text to write
Returns:
boolean indicating whether write was successful.

getPath

public static String getPath(String fname)
Returns just the path portion of a file name. If no path is present, returns "." indicating current directory.

Parameters:
fname - file name
Returns:
path portion of the file name

newer

public static boolean newer(File srcfile,
                            File destfile,
                            FilenameFilter filter)
Determines if a source file, or any file in a source directory, is newer than a destination file.

Parameters:
srcfile - source file or directory to compare
destfile - destination file to compare
filter - selects a subset of files if srcfile is a directory
Returns:
true iff --srcfile is a plain file and is newer than destfile; or --srcfile is a directory and any file in it is newer than destfile; or --destfile does not exist

newer

public static boolean newer(String srcfileName,
                            File destfile,
                            FilenameFilter filter)
Determines if a file or directory of the specified name, is newer than a destination file.

Parameters:
srcfileName - name of source file or directory to compare
destfile - destination file to compare
filter - selects a subset of files if srcfile is a directory
Returns:
true if srcfileName is newer than destfile
See Also:
newer(File, File, FilenameFilter)

newer

public static boolean newer(File srcfile,
                            File destfile)
Determines if a source file, or any file in a source directory, is newer than a destination file. If srcfile is a directory, all files are examined rather than a subset since no FilenameFilter is specified.

Parameters:
srcfile - source file or directory to compare
destfile - destination file to compare
Returns:
true if srcfile is newer than destfile
See Also:
newer(File, File, FilenameFilter)


CleanCode Java Libraries Copyright © 2001-2012 Michael Sorens - Revised 2012.12.10 Get CleanCode at SourceForge.net. Fast, secure and Free Open Source software downloads