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 SummaryFields
- Constructor SummaryConstructorsConstructorDescription- XFile- (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 SummaryModifier and TypeMethodDescription- void- close()Close stream and file.- static booleanDeletes 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.- byteGets 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.- intGets 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.- longReturns 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.- doubleReads a double value from the file.- intReads a signed long value from the file in Intel style.- intReads an signed short value from the file in Intel style.- intReads 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- monthNamesThe month names.
- dayNamesThe month names.
 
- Constructor Details- XFileOpens the specified file for read.- Throws:
- IOException- if an I/O error occurs.
 
- XFileOpens 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.
 
- XFileOpens 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.
 
- XFileOpens 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.
 
- XFileOpens a new instance of a file at a specified file position.- Throws:
- IOException- if an I/O error occurs.
 
- XFilepublic XFile- (byte[] data) Creates a new instance of a file in memory.
- XFileCreates a new instance of a file in memory.
- XFileCreates a new instance of a file in memory. The file name is taken from the current open file.
- XFileCreates a new instance of a file from an input stream, typically from ZIP/JAR files.
- XFileCreates a new instance of a file from an input stream, typically from ZIP/JAR files.
- XFileCreates a new instance of a file from an input stream, typically from ZIP/JAR files.- Throws:
- FileNotFoundException- if file is not found.
 
 
- Method Details- loadFileLoads 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.
 
- loadFileLoads 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.
 
- loadFileLoads 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.
 
- loadFileLoads 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.
 
- loadFileLoads 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.
 
- closepublic void close()Close stream and file.
- getFileNameFromAnotherReturns 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".
- getFileNameFromAnotherReturns 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.
- readReads a number of bytes from the file.- Throws:
- IOException- if an I/O error occurs.
 
- readStringReads 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:
 
- readStringReads 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:
 
- readOEMStringReads 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:
 
- readOEMStringReads 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:
 
- readUnicodeStringReads 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:
 
- readUnicodeStringReads 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.
 
- readOEMBytesReads 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:
 
- readUnicodeCharsReads 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:
 
- readByteReads an unsigned byte value from the file.- Throws:
- IOException- if an I/O error occurs.
 
- readIntelUShortReads an unsigned short value from the file in Intel style.- Throws:
- IOException- if an I/O error occurs.
 
- readIntelShortReads an signed short value from the file in Intel style.- Throws:
- IOException- if an I/O error occurs.
 
- readIntelLongReads a signed long value from the file in Intel style.- Throws:
- IOException- if an I/O error occurs.
 
- readIntelDoubleReads a double value from the file.- Throws:
- IOException- if an I/O error occurs.
 
- skipBytesSkips a number of bytes.- Throws:
- IOException- if an I/O error occurs.
 
- getRemainingSizepublic int getRemainingSize()Gets the remaining size in the memory file. It returns an estimate of what is "available()" when an input stream is used.
- getFileNameGets the file name. This method may return null if the instance of this object is created from a byte array.
- readOEMStringVectorReads the file until end as a string vector.- Throws:
- IOException- if an I/O error occurs.
 
- readOEMStringVectorpublic 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.
 
- readOEMStringVectorpublic 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.
 
- readByteLineReads 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:
 
- readOEMLineReads 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.
 
- readAnsiLineReads 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.
 
- readISOLineReads 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:
 
- getMemoryDataBytepublic byte getMemoryDataByte()Gets the current byte from the memory data (same as readByte but without advancing the position).
- findFileInPathLocates 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.
 
- findFileInPathLocates 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.
 
- findFileInDirectoryFinds 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.
 
- findFileInDirectoryFinds 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.
 
- findFilesInDirectoryFinds 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.
 
- findFilesInDirectoryFinds 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.
 
- lastModifiedpublic 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.
 
- getFileTimestampGet file time stamp as DayName Month day HH:MM:SS year.
- getFileTimestampGet file time stamp as DayName Month day HH:MM:SS year.
- getFileTimestampGet file time stamp as DayName Month day HH:MM:SS year.
- getBaseNameGets 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.
- isSameFileChecks if two file names refer to the same file, i.e. has the same canonical file names.
- isSameFileChecks if two file names refer to the same file, i.e. has the same canonical file names.
- getRelativeFileNamepublic 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
 
- getRelativePathGet 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.
 
- getRelativeFileNameGets 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.
- listDirectoryGets 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.
 
- listDirectoryGets 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.
 
- listDirectoryGets 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.
 
- listDirectoryGets 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.
 
- deleteFileAndChildrenDeletes 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).
 
- getFileExtensionGets 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!
 
- getFileExtensionGets 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!
 
- hasFileExtensionChecks 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.
 
- hasFileExtensionpublic 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.
 
- isJarOrZipFileChecks if a file is of Jar or Zip file type (i.e. has JAR or ZIP file extension, case insensitive).
- isJarOrZipFileChecks if a file is of Jar or Zip file type (i.e. has JAR or ZIP file extension, case insensitive).