Package com.iizix.util
Class XFile
java.lang.Object
com.iizix.util.XFile
This class is used for opening and reading files that are stored in OEM ASCII or Ansi character set. It also contains a few utility functions.
New functions to create a buffered FileReader and FileWriter using different encodings is now also available. See the methods createFileReader
and createFileWriter
.
- Author:
- Christopher Mindus
Field Summary
Constructor Summary
ConstructorDescriptionXFile
(byte[] data) Creates a new instance of a file in memory.Creates a new instance of a file in memory.Creates a new instance of a file in memory.Opens a new instance of a file at a specified file position.Creates a new instance of a file from an input stream, typically from ZIP/JAR files.XFile
(InputStream in) Creates a new instance of a file from an input stream, typically from ZIP/JAR files.XFile
(InputStream in, String fileName) Creates a new instance of a file from an input stream, typically from ZIP/JAR files.Opens the specified file for read.Opens the specified file for read.Opens the specified file for read.Opens the specified file for read.Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close stream and file.static boolean
Deletes a file or directory along with all its children (in case of a directory).static String
findFileInDirectory
(File dir, String fileName, boolean matchCase) Finds a file in a directory or in one of it's sub-directories.static String
findFileInDirectory
(String dir, String fileName, boolean matchCase) Finds a file in a directory or in one of it's sub-directories.static String
findFileInPath
(String fileName) Locates the specified file.static String
findFileInPath
(String fileName, boolean doSearchPath) Locates the specified file.static int
findFilesInDirectory
(File dir, ArrayList<String> fileNames, boolean matchCase) Finds a file in a directory or in one of it's sub-directories.static int
findFilesInDirectory
(String dir, ArrayList<String> fileNames, boolean matchCase) Finds a file in a directory or in one of it's sub-directories.static String
getBaseName
(String fn) Gets the base file name without path and file extension.static String
getFileExtension
(File file) Gets the extension of a file.static String
getFileExtension
(String fileName) Gets the extension of a file.Gets the file name.getFileNameFromAnother
(String base) Returns a file name and path that is taken from the current XFile instance with path, but with the base name of the file name in XFile.static String
getFileNameFromAnother
(String base, String another) Returns a file name and path that is taken from a file with path, but with the base name of another file.Get file time stamp as DayName Month day HH:MM:SS year.static String
getFileTimestamp
(long lastModified) Get file time stamp as DayName Month day HH:MM:SS year.static String
getFileTimestamp
(String fileName) Get file time stamp as DayName Month day HH:MM:SS year.byte
Gets the current byte from the memory data (same as readByte but without advancing the position).static String
getRelativeFileName
(String fileName, String reference) Gets the relative file name from another file (if possible).static String
getRelativeFileName
(String fileName, String reference, boolean mustBeChildOrSibling, boolean stripRelativePath, boolean mustExist, boolean mustBeFile) Gets a file name relative to a reference file name.static String
getRelativePath
(File home, File f) Get relative path of File 'f' with respect to 'home' directory.int
Gets the remaining size in the memory file.static boolean
hasFileExtension
(File file, boolean caseSensitive, String... extensions) Checks the extension of a file for a valid one with the ones specified in the list of extensions.static boolean
hasFileExtension
(String fileName, boolean caseSensitive, String... extensions) Checks the extension of a file for a valid one with the ones specified in the list of extensions.static boolean
isJarOrZipFile
(File file) Checks if a file is of Jar or Zip file type (i.e.static boolean
isJarOrZipFile
(String fileName) Checks if a file is of Jar or Zip file type (i.e.static boolean
isSameFile
(File f1, File f2) Checks if two file names refer to the same file, i.e.static boolean
isSameFile
(String fn1, String fn2) Checks if two file names refer to the same file, i.e.long
Returns the time that the file represented by this file object was last modified.static boolean
listDirectory
(File dir, ArrayList<File> array) Gets a directory listing including all children and sub-directories.static boolean
listDirectory
(File dir, ArrayList<File> array, boolean includeDirectories) Gets a directory listing including all children and sub-directories.static boolean
listDirectory
(String dir, ArrayList<File> array) Gets a directory listing including all children and sub-directories.static boolean
listDirectory
(String dir, ArrayList<File> array, boolean includeDirectories) Gets a directory listing including all children and sub-directories.static byte[]
Loads a file into memory.static byte[]
Loads a file into memory.static byte[]
loadFile
(InputStream in) Loads a file into memory.static byte[]
Loads a file into memory.static byte[]
Loads a file into memory.byte[]
read
(int size) Reads a number of bytes from the file.Reads a line in the file up to the next new line (CR/LF or just LF) or the end of the file.int
readByte()
Reads an unsigned byte value from the file.byte[]
Reads a line in the file up to the next new line (CR/LF or just LF) or the end of the file.double
Reads a double value from the file.int
Reads a signed long value from the file in Intel style.int
Reads an signed short value from the file in Intel style.int
Reads an unsigned short value from the file in Intel style.Reads a line in the file up to the next new line (CR/LF or just LF) or the end of the file.readOEMBytes
(int size) Reads a number of OEM bytes and returns it as a string.Reads a line in the file up to the next new line (CR/LF or just LF) or the end of the file.Reads a number of OEM bytes and returns it as a string.readOEMString
(int size) Reads a number of OEM bytes and returns it as a string.readOEMStringVector
(boolean skipEmptyLines) Reads the file until end as a string vector.readOEMStringVector
(boolean skipEmptyLines, boolean trimLines) Reads the file until end as a string vector.readOEMStringVector
(boolean skipEmptyLines, boolean trimLeading, boolean trimTrailing) Reads the file until end as a string vector.Reads a number of bytes and returns it as a string.readString
(int size) Reads a number of bytes and returns it as a string.readUnicodeChars
(int size) Reads a number of Unicode characters (16-bit words) and returns it as a string.Reads a number of Unicode characters (16-bit words) and returns it as a string.readUnicodeString
(int size) Reads a number of Unicode characters (16-bit words) and returns it as a string.void
skipBytes
(int n) Skips a number of bytes.
Field Details
monthNames
The month names.dayNames
The month names.
Constructor Details
XFile
Opens the specified file for read.- Throws:
IOException
- if an I/O error occurs.
XFile
Opens the specified file for read. The file can be located in the Java Class Path, but not in any archives (zip/jar). If the file name contains a relative or absolute path, the path will not be searched.- Throws:
IOException
- if an I/O error occurs.
XFile
Opens the specified file for read. The directory of the file name is taken from the instance of the XFile passed into this method.- Throws:
IOException
- if an I/O error occurs.
XFile
Opens the specified file for read. The directory of the file name is taken from the file name passed into this method.- Throws:
IOException
- if an I/O error occurs.
XFile
Opens a new instance of a file at a specified file position.- Throws:
IOException
- if an I/O error occurs.
XFile
public XFile(byte[] data) Creates a new instance of a file in memory.XFile
Creates a new instance of a file in memory.XFile
Creates a new instance of a file in memory. The file name is taken from the current open file.XFile
Creates a new instance of a file from an input stream, typically from ZIP/JAR files.XFile
Creates a new instance of a file from an input stream, typically from ZIP/JAR files.XFile
Creates a new instance of a file from an input stream, typically from ZIP/JAR files.- Throws:
FileNotFoundException
- if file is not found.
Method Details
loadFile
Loads a file into memory.- Parameters:
in
- the input stream.- Returns:
- null for out of memory.
- Throws:
FileNotFoundException
- if the file cannot be found.IOException
- if the file cannot be loaded.
loadFile
Loads a file into memory.- Returns:
- null for out of memory.
- Throws:
FileNotFoundException
- if the file cannot be found.IOException
- if the file cannot be loaded.
loadFile
Loads a file into memory.- Returns:
- null for out of memory.
- Throws:
FileNotFoundException
- if the file cannot be found.IOException
- if the file cannot be loaded.
loadFile
Loads a file into memory.- Returns:
- null for out of memory.
- Throws:
FileNotFoundException
- if the file cannot be found.IOException
- if the file cannot be loaded.
loadFile
Loads a file into memory.- Returns:
- null for out of memory.
- Throws:
FileNotFoundException
- if the file cannot be found.IOException
- if the file cannot be loaded.
close
public void close()Close stream and file.getFileNameFromAnother
Returns a file name and path that is taken from a file with path, but with the base name of another file. If the new base file name starts with a file name separator as "/path/filename", and is therefore not relative as "./path/filename" or "path/filename", the path of the other file is not used. This is also valid for drive specification such as "c:\path\filename" or "unit:/path/filename".getFileNameFromAnother
Returns a file name and path that is taken from the current XFile instance with path, but with the base name of the file name in XFile.read
Reads a number of bytes from the file.- Throws:
IOException
- if an I/O error occurs.
readString
Reads a number of bytes and returns it as a string. The string ends if a zero-termination character is encountered. The string is assumed to be in default Ansi code page.- Throws:
IOException
- if an I/O error occurs.- See Also:
readString
Reads a number of bytes and returns it as a string. The string ends if a zero-termination character is encountered. The string is assumed to be in default Ansi code page. Note: The entire string is read from the file as specified by the size.- Throws:
IOException
- if an I/O error occurs.- See Also:
readOEMString
Reads a number of OEM bytes and returns it as a string. The string ends if a zero-termination character is encountered. Note: conversion is done from the current code page to Unicode.- Throws:
IOException
- if an I/O error occurs.- See Also:
readOEMString
Reads a number of OEM bytes and returns it as a string. The string ends if a zero-termination character is encountered. Note: conversion is done from the current code page to Unicode. Note: The entire string is read from the file as specified by the size.- Throws:
IOException
- if an I/O error occurs.- See Also:
readUnicodeString
Reads a number of Unicode characters (16-bit words) and returns it as a string. The string ends as soon as a zero-character is read.- Throws:
IOException
- if an I/O error occurs.- See Also:
readUnicodeString
Reads a number of Unicode characters (16-bit words) and returns it as a string. The string ends as soon as a zero-character is read.- Throws:
IOException
- if an I/O error occurs.
readOEMBytes
Reads a number of OEM bytes and returns it as a string. Note: conversion is done from the current code page to Unicode.- Throws:
IOException
- if an I/O error occurs.- See Also:
readUnicodeChars
Reads a number of Unicode characters (16-bit words) and returns it as a string. The string ends as soon as a zero-character is read.- Throws:
IOException
- if an I/O error occurs.- See Also:
readByte
Reads an unsigned byte value from the file.- Throws:
IOException
- if an I/O error occurs.
readIntelUShort
Reads an unsigned short value from the file in Intel style.- Throws:
IOException
- if an I/O error occurs.
readIntelShort
Reads an signed short value from the file in Intel style.- Throws:
IOException
- if an I/O error occurs.
readIntelLong
Reads a signed long value from the file in Intel style.- Throws:
IOException
- if an I/O error occurs.
readIntelDouble
Reads a double value from the file.- Throws:
IOException
- if an I/O error occurs.
skipBytes
Skips a number of bytes.- Throws:
IOException
- if an I/O error occurs.
getRemainingSize
public int getRemainingSize()Gets the remaining size in the memory file. It returns an estimate of what is "available()" when an input stream is used.getFileName
Gets the file name. This method may return null if the instance of this object is created from a byte array.readOEMStringVector
Reads the file until end as a string vector.- Throws:
IOException
- if an I/O error occurs.
readOEMStringVector
public Vector<String> readOEMStringVector(boolean skipEmptyLines, boolean trimLines) throws IOException Reads the file until end as a string vector.- Throws:
IOException
- if an I/O error occurs.
readOEMStringVector
public Vector<String> readOEMStringVector(boolean skipEmptyLines, boolean trimLeading, boolean trimTrailing) throws IOException Reads the file until end as a string vector.- Throws:
IOException
- if an I/O error occurs.
readByteLine
Reads a line in the file up to the next new line (CR/LF or just LF) or the end of the file.- Returns:
- null for end of file.
- Throws:
IOException
- if an I/O error occurs.- See Also:
readOEMLine
Reads a line in the file up to the next new line (CR/LF or just LF) or the end of the file.- Returns:
- null for end of file.
- Throws:
IOException
- if an I/O error occurs.
readAnsiLine
Reads a line in the file up to the next new line (CR/LF or just LF) or the end of the file.- Returns:
- null for end of file.
- Throws:
IOException
- if an I/O error occurs.
readISOLine
Reads a line in the file up to the next new line (CR/LF or just LF) or the end of the file. The data in the file should be encoded in ISO 8859-1.Typically, this function is used to read data that have been written with XOutputFile.writeISOLine.
- Returns:
- null for end of file.
- Throws:
IOException
- if an I/O error occurs.- See Also:
getMemoryDataByte
public byte getMemoryDataByte()Gets the current byte from the memory data (same as readByte but without advancing the position).findFileInPath
Locates the specified file. The file can be located in the Java Class Path, but not in any archives (zip/jar). If the file name contains a relative or absolute path, the path will not be searched.- Returns:
- null if the file is not found.
findFileInPath
Locates the specified file. The file can be located in the Java Class Path, but not in any archives (zip/jar). If the file name contains a relative or absolute path, the path will not be searched.- Returns:
- null if the file is not found.
findFileInDirectory
Finds a file in a directory or in one of it's sub-directories. This function performs variable substitution such as "%ProgramFiles%\firefox.exe" to search for the FireFox executable in the program files folder defined for the computer in the environment.- Returns:
- the full path to the file, or null if not found or for errors in the variable substitution.
findFileInDirectory
Finds a file in a directory or in one of it's sub-directories.- Returns:
- the full path to the file, or null if not found.
findFilesInDirectory
Finds a file in a directory or in one of it's sub-directories. This function performs variable substitution such as "%ProgramFiles%\firefox.exe" to search for the FireFox executable in the program files folder defined for the computer in the environment. The parameter "dirDisplay" can be null [not present in XFile yet!].- Returns:
- The count of files found, the original list is modified.
findFilesInDirectory
Finds a file in a directory or in one of it's sub-directories. The parameter "dirDisplay" can be null [not present in XFile yet!].- Returns:
- the count of files found.
lastModified
public long lastModified()Returns the time that the file represented by this file object was last modified. The return value is system dependent and should only be used to compare with other values returned by last modified. It should not be interpreted as an absolute time.- Returns:
- the time the file specified by this object was last modified, or 0L if the specified file does not exist.
getFileTimestamp
Get file time stamp as DayName Month day HH:MM:SS year.getFileTimestamp
Get file time stamp as DayName Month day HH:MM:SS year.getFileTimestamp
Get file time stamp as DayName Month day HH:MM:SS year.getBaseName
Gets the base file name without path and file extension. This is typically used for the package name for Java code. The case of the base name is unchanged.isSameFile
Checks if two file names refer to the same file, i.e. has the same canonical file names.isSameFile
Checks if two file names refer to the same file, i.e. has the same canonical file names.getRelativeFileName
public static String getRelativeFileName(String fileName, String reference, boolean mustBeChildOrSibling, boolean stripRelativePath, boolean mustExist, boolean mustBeFile) throws IOException, FileNotFoundException Gets a file name relative to a reference file name. The flag "mustBeChildOrSibling" is used to check that the file name is relative the reference, but is not "above" it. The files must exist.- Returns:
- null for failure.
- Throws:
IOException
FileNotFoundException
getRelativePath
Get relative path of File 'f' with respect to 'home' directory.example : home = /a/b/c f = /a/d/e/x.txt s = getRelativePath(home,f) = ../../d/e/x.txt
- Parameters:
home
- base path, should be a directory, not a file, or it doesn't make sense.f
- file to generate path for.- Returns:
- path from home to f as a string
- Throws:
IOException
- for file failures.
getRelativeFileName
Gets the relative file name from another file (if possible). The parameter "reference" can be seen as the "home" file or directory to make the other file ("filename") relative to.listDirectory
Gets a directory listing including all children and sub-directories. Only files are added.- Parameters:
dir
- The directory to list.array
- The array of files and perhaps directories found.- Returns:
- true for success, false for failure.
listDirectory
Gets a directory listing including all children and sub-directories.- Parameters:
dir
- The directory to list.array
- The array of files and perhaps directories found.includeDirectories
- Flag to include directories if true, false for only files.- Returns:
- true for success, false for failure.
listDirectory
Gets a directory listing including all children and sub-directories. Only files are added.- Parameters:
dir
- The directory to list.array
- The array of files and perhaps directories found.- Returns:
- true for success, false for failure.
listDirectory
Gets a directory listing including all children and sub-directories.- Parameters:
dir
- The directory to list.array
- The array of files and perhaps directories found.includeDirectories
- Flag to include directories if true, false for only files.- Returns:
- true for success, false for failure.
deleteFileAndChildren
Deletes a file or directory along with all its children (in case of a directory).- Returns:
- true for success, false for failure (some file/directory may remain, but as much as possible is deleted).
getFileExtension
Gets the extension of a file. The return value is the extension of the file without the last dot. This functions doesn't make a difference with the OS implementations "\" or "/", both are accepted!- Parameters:
file
- the file.- Returns:
- null when no file extension is found!
getFileExtension
Gets the extension of a file. The return value is the extension of the file without the last dot. This functions doesn't make a difference with the OS implementations "\" or "/", both are accepted!- Parameters:
fileName
- the file name.- Returns:
- null when no file extension is found!
hasFileExtension
Checks the extension of a file for a valid one with the ones specified in the list of extensions. The option "caseSensitive" can be set if required. This functions doesn't make a difference with the OS implementations "\" or "/", both are accepted!- Parameters:
file
- the file.caseSensitive
- if case sensitivity is used or not.extensions
- a variable length of file extensions without the ".", e.g. "html","htm" for HTML files.- Returns:
- true if the file has one of the file extensions, false otherwise.
hasFileExtension
public static boolean hasFileExtension(String fileName, boolean caseSensitive, String... extensions) Checks the extension of a file for a valid one with the ones specified in the list of extensions. The option "caseSensitive" can be set if required. This functions doesn't make a difference with the OS implementations "\" or "/", both are accepted!- Parameters:
fileName
- the file name.caseSensitive
- if case sensitivity is used or not.extensions
- a variable length of file extensions without the ".", e.g. "html","htm" for HTML files.- Returns:
- true if the file has one of the file extensions, false otherwise.
isJarOrZipFile
Checks if a file is of Jar or Zip file type (i.e. has JAR or ZIP file extension, case insensitive).isJarOrZipFile
Checks if a file is of Jar or Zip file type (i.e. has JAR or ZIP file extension, case insensitive).