Terrier IR Platform
2.2.1

uk.ac.gla.terrier.utility
Class Files

java.lang.Object
  extended by uk.ac.gla.terrier.utility.Files

public class Files
extends java.lang.Object

Utililty class for opening readers/writers and input/output streams to files. Handles gzipped files on the fly, ie if a file ends in ".gz" or ".GZ", then it will be opened using a GZipInputStream/GZipOutputStream. All returned Streams, Readers, Writers etc are Buffered. If a charset encoding is not specified, then the system default is used. New interfaces are used to descibe random data access.

FileSystem plugsin

Additional file systems can be plugged into this module, by calling the addFileSystemCapability() method. FileSystems have read and/or write capabilities, as specified using the FSCapability constants. Files using these external file systems should be denoted by scheme prefixes - eg ftp://, http:// etc. NB: file:// is the default scheme


Nested Class Summary
static interface Files.FSCapability
          constants declaring which capabilites a file system has
 
Constructor Summary
Files()
           
 
Method Summary
static void addFileSystemCapability(FileSystem fs)
          Add a file system to Terrier.
static void addPathTransormation(java.lang.String find, java.lang.String replace)
          add a static transformation to apply to a path.
static boolean canRead(java.lang.String filename)
          returns true iff path can be read
static boolean canWrite(java.lang.String filename)
          returns true iff path can be read
static java.lang.Long copyFile(java.io.File srcFile, java.io.File destFile)
          Copy a file from srcFile to destFile.
static java.lang.Long copyFile(java.io.InputStream in, java.io.OutputStream out)
          Copy all bytes from in to out
static java.lang.Long copyFile(java.lang.String srcFilename, java.lang.String destFilename)
          Copy a file from srcFile to destFile.
static java.lang.Long createChecksum(java.io.File file)
          Returns the CRC checksum of denoted file
static boolean delete(java.lang.String filename)
          Delete the named file.
static boolean deleteOnExit(java.lang.String path)
          Mark the named path as to be deleted on exit.
static boolean exists(java.lang.String path)
          returns true iff the path is really a path
static java.lang.String getFileSystemName(java.lang.String path)
          Get the name of the file system that would be used to access a given file or directory.
static java.lang.String getParent(java.lang.String path)
          What is the parent path to the specified path?
static boolean isDirectory(java.lang.String path)
          return true if path is a directory
static long length(java.io.File f)
          returns the length of file f
static long length(java.lang.String filename)
          returns the length of the file, or 0L if cannot be found etc
static java.lang.String[] list(java.lang.String path)
          List the contents of a directory
static boolean mkdir(java.lang.String path)
          returns true if the specificed path can be made as a directory
static RandomDataInput openFileRandom(java.io.File file)
          Open a file for random access reading
static RandomDataInput openFileRandom(java.lang.String filename)
          Returns a RandomAccessFile implementation accessing the specificed file
static java.io.BufferedReader openFileReader(java.io.File file)
          Opens a reader to the file called file.
static java.io.BufferedReader openFileReader(java.io.File file, java.lang.String charset)
          Opens a reader to the file called filename.
static java.io.BufferedReader openFileReader(java.lang.String filename)
          Opens a reader to the file called filename.
static java.io.BufferedReader openFileReader(java.lang.String filename, java.lang.String charset)
          Opens a reader to the file called filename.
static java.io.InputStream openFileStream(java.io.File file)
          Opens an InputStream to a file called file.
static java.io.InputStream openFileStream(java.lang.String filename)
          Opens an InputStream to a file called filename.
static boolean rename(java.lang.String sourceFilename, java.lang.String destFilename)
          rename a file or directory.
static RandomDataOutput writeFileRandom(java.io.File file)
          Open a file for random access writing and reading
static RandomDataOutput writeFileRandom(java.lang.String filename)
          Returns a RandomAccessFile implementation accessing the specificed file
static java.io.OutputStream writeFileStream(java.io.File file)
          Opens an OutputStream to a file called file.
static java.io.OutputStream writeFileStream(java.lang.String filename)
          Opens an OutputStream to a file called filename.
static java.io.Writer writeFileWriter(java.io.File file)
          Opens an Writer to a file called file.
static java.io.Writer writeFileWriter(java.io.File file, java.lang.String charset)
          Opens an Writer to a file called file.
static java.io.Writer writeFileWriter(java.lang.String filename)
          Opens an Writer to a file called file.
static java.io.Writer writeFileWriter(java.lang.String filename, java.lang.String charset)
          Opens an Writer to a file called file.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Files

public Files()
Method Detail

addPathTransormation

public static void addPathTransormation(java.lang.String find,
                                        java.lang.String replace)
add a static transformation to apply to a path. Find and replace are both regular expressions


addFileSystemCapability

public static void addFileSystemCapability(FileSystem fs)
Add a file system to Terrier. File systems are denoted by URI scheme prefixes (e.g. http). The underlying file system is represented by an FileSystem


getFileSystemName

public static java.lang.String getFileSystemName(java.lang.String path)
Get the name of the file system that would be used to access a given file or directory.

Parameters:
path -
Returns:
name Name of the file system, or null if no filesystem found

openFileRandom

public static RandomDataInput openFileRandom(java.lang.String filename)
                                      throws java.io.IOException
Returns a RandomAccessFile implementation accessing the specificed file

Throws:
java.io.IOException

writeFileRandom

public static RandomDataOutput writeFileRandom(java.lang.String filename)
                                        throws java.io.IOException
Returns a RandomAccessFile implementation accessing the specificed file

Throws:
java.io.IOException

delete

public static boolean delete(java.lang.String filename)
Delete the named file. Returns false if the scheme of filename cannot be recognised, the filesystem doesnt have write capability, or the underlying filesystem could not delete the file

Parameters:
filename - path to file to delete

deleteOnExit

public static boolean deleteOnExit(java.lang.String path)
Mark the named path as to be deleted on exit. Returns false if the scheme of the filename cannot be recognised, the filesystem does not have write capability, or the file system does not have deleteOnExit capability


exists

public static boolean exists(java.lang.String path)
returns true iff the path is really a path


canRead

public static boolean canRead(java.lang.String filename)
returns true iff path can be read


canWrite

public static boolean canWrite(java.lang.String filename)
returns true iff path can be read


mkdir

public static boolean mkdir(java.lang.String path)
returns true if the specificed path can be made as a directory


length

public static long length(java.lang.String filename)
returns the length of the file, or 0L if cannot be found etc


isDirectory

public static boolean isDirectory(java.lang.String path)
return true if path is a directory


rename

public static boolean rename(java.lang.String sourceFilename,
                             java.lang.String destFilename)
rename a file or directory. If the two are on different file systems, it is assumed to be a file


getParent

public static java.lang.String getParent(java.lang.String path)
What is the parent path to the specified path?


list

public static java.lang.String[] list(java.lang.String path)
List the contents of a directory


openFileReader

public static java.io.BufferedReader openFileReader(java.io.File file)
                                             throws java.io.IOException
Opens a reader to the file called file. Provided for easy overriding for encoding support etc in child classes. Called from openNextFile().

Parameters:
file - File to open.
Returns:
BufferedReader of the file
Throws:
java.io.IOException

openFileReader

public static java.io.BufferedReader openFileReader(java.io.File file,
                                                    java.lang.String charset)
                                             throws java.io.IOException
Opens a reader to the file called filename. Provided for easy overriding for encoding support etc in child classes. Called from openNextFile().

Parameters:
file - File to open.
charset - Character set encoding of file. null for system default.
Returns:
BufferedReader of the file
Throws:
java.io.IOException

openFileReader

public static java.io.BufferedReader openFileReader(java.lang.String filename)
                                             throws java.io.IOException
Opens a reader to the file called filename. Provided for easy overriding for encoding support etc in child classes. Called from openNextFile().

Parameters:
filename - File to open.
Returns:
BufferedReader of the file
Throws:
java.io.IOException

openFileReader

public static java.io.BufferedReader openFileReader(java.lang.String filename,
                                                    java.lang.String charset)
                                             throws java.io.IOException
Opens a reader to the file called filename. Provided for easy overriding for encoding support etc in child classes. Called from openNextFile().

Parameters:
filename - File to open.
charset - Character set encoding of file. null for system default.
Returns:
BufferedReader of the file
Throws:
java.io.IOException

openFileStream

public static java.io.InputStream openFileStream(java.io.File file)
                                          throws java.io.IOException
Opens an InputStream to a file called file.

Parameters:
file - File to open.
Returns:
InputStream of the file
Throws:
java.io.IOException

openFileRandom

public static RandomDataInput openFileRandom(java.io.File file)
                                      throws java.io.IOException
Open a file for random access reading

Throws:
java.io.IOException

openFileStream

public static java.io.InputStream openFileStream(java.lang.String filename)
                                          throws java.io.IOException
Opens an InputStream to a file called filename.

Parameters:
filename - File to open.
Returns:
InputStream of the file
Throws:
java.io.IOException

writeFileStream

public static java.io.OutputStream writeFileStream(java.io.File file)
                                            throws java.io.IOException
Opens an OutputStream to a file called file.

Parameters:
file - File to open.
Returns:
OutputStream of the file
Throws:
java.io.IOException

writeFileRandom

public static RandomDataOutput writeFileRandom(java.io.File file)
                                        throws java.io.IOException
Open a file for random access writing and reading

Throws:
java.io.IOException

writeFileStream

public static java.io.OutputStream writeFileStream(java.lang.String filename)
                                            throws java.io.IOException
Opens an OutputStream to a file called filename.

Parameters:
filename - File to open.
Returns:
OutputStream of the file
Throws:
java.io.IOException

writeFileWriter

public static java.io.Writer writeFileWriter(java.io.File file)
                                      throws java.io.IOException
Opens an Writer to a file called file. System default encoding will be used.

Parameters:
file - File to open.
Returns:
Writer of the file
Throws:
java.io.IOException

writeFileWriter

public static java.io.Writer writeFileWriter(java.io.File file,
                                             java.lang.String charset)
                                      throws java.io.IOException
Opens an Writer to a file called file.

Parameters:
file - File to open.
charset - Character set encoding of file. null for system default.
Returns:
Writer of the file
Throws:
java.io.IOException

writeFileWriter

public static java.io.Writer writeFileWriter(java.lang.String filename)
                                      throws java.io.IOException
Opens an Writer to a file called file. System default encoding will be used.

Parameters:
filename - File to open.
Returns:
Writer of the file
Throws:
java.io.IOException

writeFileWriter

public static java.io.Writer writeFileWriter(java.lang.String filename,
                                             java.lang.String charset)
                                      throws java.io.IOException
Opens an Writer to a file called file.

Parameters:
filename - File to open.
charset - Character set encoding of file. null for system default.
Returns:
Writer of the file
Throws:
java.io.IOException

copyFile

public static java.lang.Long copyFile(java.lang.String srcFilename,
                                      java.lang.String destFilename)
                               throws java.io.IOException
Copy a file from srcFile to destFile.

Returns:
null if OK
Throws:
java.io.IOException - if there was a problem copying

copyFile

public static java.lang.Long copyFile(java.io.File srcFile,
                                      java.io.File destFile)
                               throws java.io.IOException
Copy a file from srcFile to destFile.

Returns:
null if OK
Throws:
java.io.IOException - if there was a problem copying

copyFile

public static java.lang.Long copyFile(java.io.InputStream in,
                                      java.io.OutputStream out)
                               throws java.io.IOException
Copy all bytes from in to out

Returns:
null if OK throws IOException if there was a problem copying
Throws:
java.io.IOException

createChecksum

public static java.lang.Long createChecksum(java.io.File file)
                                     throws java.io.IOException
Returns the CRC checksum of denoted file

Throws:
java.io.IOException

length

public static long length(java.io.File f)
returns the length of file f


Terrier IR Platform
2.2.1

Terrier Information Retrieval Platform 2.2.1. Copyright 2004-2008 University of Glasgow