com.marringtons.file
Class FileReader

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

public class FileReader
extends Object

This class provides simple access to read a text file that is found on one of the system paths or Zip files. A file within a specific archive can be specified with the special /#/ syntax to separate zip file from content. Use close when completed with the stream (not needed by load or readLines).

 FileReader fileReader = new FileReader( "/com/fred/project/myFile.txt");
 ... read from file
 fileReader.close();
 
 FileReader nextReader = new FileReader( "myFile2.txt", fileReader);
 fileReader.close();
 fileReader = nextReader;
 // same as... but open preferred to working the garbage collector
 if (fileReader.open( "myFile2.txt")) ...
 if (fileReader.open( "/com/fred/project/myFile2.txt")) ...
 if (fileReader.open( "file:///etc/hosts")) ...
 if (fileReader.open( "file://c:/winnt/system32/drivers/etc/hosts")) ...
 
 if (fileReader.open( "myArchive.zip/#/myFile2.txt")) ...
 // same as...
 fileReader.setArchive( "myArchive.zip");
 if (fileReader.open( "myFile2.txt")) ...
 
 // or we can get a list of archive entries
 String[] files = fileReader.listArchive();
 fileReader.setArchive( null); so can be used for non-archive again
 
 
 String line;
 while ((line = fileReader.getLine()) != null) processLine();
 fileReader.close();
 
 byte[] contents = fileReader.load();
 // no close needed
 

Author:
Paul Marrington

Constructor Summary
FileReader()
          Default constructor.
FileReader(String filePath)
          Search for a file on all datapaths, given the absolute (to datapath) name.
FileReader(String filePath, boolean mustExist)
          In some circumstances it is an error if the file does not exists.
 
Method Summary
 void close()
          Close BufferedReader object.
 String getClassName()
          Assuming that the file is actually a Java class file, extract the class name from the file name.
 File getFile()
          Retrieve a pointer to the underlying file.
 InputStream getInputStream()
          Retrieve a pointer to the underlying input stream.
 BufferedReader getReader()
          Buffered reader used internally or externally when more sophisticated reading is required.
 boolean isArchived()
          Used if you need to know if a file exists in the file system or part of an archive.
 boolean isOpen()
          See if the file is open.
 String[] listArchive()
          Retrieve a full list of file entries in an archive
 byte[] load()
          Loads the file as one chunk.
 boolean open(String filePath)
          Search for a file on all datapaths, given the relative or absolute (to datapath) name.
 String readLine()
          Read a line from the file, opening it if necessary.
static String[] readLines(String name)
          Reads the entire file and returns it in an array of lines.
 void setArchive(String archiveName)
          Set the archive used for the next open statement.
static void setZipFileCacheSize(int size)
          Only ever called by Application.load() to reset the zip file cache size to overcome chicken-and-egg problem.
 int size()
          Retrieve the size of the opened file.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileReader

public FileReader()
Default constructor. File to be opened later.


FileReader

public FileReader(String filePath)
Search for a file on all datapaths, given the absolute (to datapath) name. Datapaths include zip and class files on the classpath and command line.

Parameters:
filePath - - path relative to default paths (not absolute to filesystem)

FileReader

public FileReader(String filePath,
                  boolean mustExist)
           throws FileNotFoundException
In some circumstances it is an error if the file does not exists. This overload us used at those times/

Parameters:
filePath -
mustExist - true to throw an error if the file is not found
Throws:
FileNotFoundException
Method Detail

open

public boolean open(String filePath)
Search for a file on all datapaths, given the relative or absolute (to datapath) name. Datapaths include zip and class files on the classpath and command line.

Parameters:
filePath - path relative to default paths (not absolute to filesystem). It can also be part of a specified archive if setArchive is called first or if the path includes /#/ to separate the archive from file path components.
Returns:
boolean true if file open and ready to read

setArchive

public void setArchive(String archiveName)
Set the archive used for the next open statement. If it is on the datapath then the filebase will be used - otherwise the path may be absolute within the archive.

Parameters:
archiveName - absolute or relative name of archive file or null for no default archive.

listArchive

public String[] listArchive()
                     throws IOException
Retrieve a full list of file entries in an archive

Returns:
String array with an item for each archive entry.
Throws:
IOException

getReader

public BufferedReader getReader()
Buffered reader used internally or externally when more sophisticated reading is required.

Returns:
buffered reader or null if the file cannot be opened.

isOpen

public boolean isOpen()
See if the file is open.

Returns:
boolean returns true if reader is open and ready to be read.

isArchived

public boolean isArchived()
Used if you need to know if a file exists in the file system or part of an archive.

Returns:
true if file is part of an archive

close

public void close()
Close BufferedReader object. Use when file reading is complete. See delete() if object is not to be used as basis for another object.


readLine

public String readLine()
                throws IOException
Read a line from the file, opening it if necessary.

Returns:
String next line from file (or NULL on end of file)
Throws:
IOException

readLines

public static String[] readLines(String name)
Reads the entire file and returns it in an array of lines.

Parameters:
name - of file to read
Returns:
array of lines - empty if file does not exist.

load

public byte[] load()
            throws IOException
Loads the file as one chunk. Used by class loaders. Closes the file after the read.

Returns:
the complete file as an array of bytes
Throws:
IOException

getInputStream

public InputStream getInputStream()
Retrieve a pointer to the underlying input stream.

Returns:
InputStream or null of no file open for reading

getFile

public File getFile()
Retrieve a pointer to the underlying file.

Returns:
File object used for this file

size

public int size()
Retrieve the size of the opened file.

Returns:
int length of the opened file in bytes.

setZipFileCacheSize

public static void setZipFileCacheSize(int size)
Only ever called by Application.load() to reset the zip file cache size to overcome chicken-and-egg problem.

Parameters:
size - new size. Only used if greater than 10.

getClassName

public String getClassName()
Assuming that the file is actually a Java class file, extract the class name from the file name.

Returns:
class name (no .class, . instead of /).


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