Class XFile

java.lang.Object
com.iizix.util.XFile

public class XFile extends Object
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 Details

    • monthNames

      public static final String[] monthNames
      The month names.
    • dayNames

      public static final String[] dayNames
      The month names.
  • Constructor Details

    • XFile

      public XFile(String fileName) throws IOException
      Opens the specified file for read.
      Throws:
      IOException - if an I/O error occurs.
    • XFile

      public XFile(String fileName, boolean doSearchPath) throws IOException
      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

      public XFile(String fileName, XFile openFile) throws IOException
      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

      public XFile(String fileName, String previousFile) throws IOException
      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

      public XFile(XFile file, int position) throws IOException
      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

      public XFile(byte[] data, String fileName)
      Creates a new instance of a file in memory.
    • XFile

      public XFile(byte[] data, XFile openFile)
      Creates a new instance of a file in memory. The file name is taken from the current open file.
    • XFile

      public XFile(InputStream in)
      Creates a new instance of a file from an input stream, typically from ZIP/JAR files.
    • XFile

      public XFile(InputStream in, String fileName)
      Creates a new instance of a file from an input stream, typically from ZIP/JAR files.
    • XFile

      public XFile(File file) throws FileNotFoundException
      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

      public static byte[] loadFile(InputStream in) throws IOException
      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

      public static byte[] loadFile(XFile file) throws IOException
      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

      public static byte[] loadFile(String fileName) throws IOException
      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

      public static byte[] loadFile(File file) throws IOException
      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

      public static byte[] loadFile(JarFile jar, JarEntry je) throws IOException
      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

      public 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. 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

      public String 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.
    • read

      public byte[] read(int size) throws IOException
      Reads a number of bytes from the file.
      Throws:
      IOException - if an I/O error occurs.
    • readString

      public String readString() throws IOException
      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

      public String readString(int size) throws IOException
      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

      public String readOEMString() throws IOException
      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

      public String readOEMString(int size) throws IOException
      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

      public String readUnicodeString(int size) throws IOException
      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

      public String readUnicodeString() throws IOException
      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

      public String readOEMBytes(int size) throws IOException
      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

      public String readUnicodeChars(int size) throws IOException
      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

      public int readByte() throws IOException
      Reads an unsigned byte value from the file.
      Throws:
      IOException - if an I/O error occurs.
    • readIntelUShort

      public int readIntelUShort() throws IOException
      Reads an unsigned short value from the file in Intel style.
      Throws:
      IOException - if an I/O error occurs.
    • readIntelShort

      public int readIntelShort() throws IOException
      Reads an signed short value from the file in Intel style.
      Throws:
      IOException - if an I/O error occurs.
    • readIntelLong

      public int readIntelLong() throws IOException
      Reads a signed long value from the file in Intel style.
      Throws:
      IOException - if an I/O error occurs.
    • readIntelDouble

      public double readIntelDouble() throws IOException
      Reads a double value from the file.
      Throws:
      IOException - if an I/O error occurs.
    • skipBytes

      public void skipBytes(int n) throws IOException
      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

      public String getFileName()
      Gets the file name. This method may return null if the instance of this object is created from a byte array.
    • readOEMStringVector

      public Vector<String> readOEMStringVector(boolean skipEmptyLines) 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 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

      public byte[] readByteLine() throws IOException
      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

      public String readOEMLine() throws IOException
      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

      public String readAnsiLine() throws IOException
      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

      public String readISOLine() throws IOException
      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

      public static String findFileInPath(String fileName)
      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

      public static String findFileInPath(String fileName, boolean doSearchPath)
      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

      public static String findFileInDirectory(String dir, String fileName, boolean matchCase)
      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

      public static String findFileInDirectory(File dir, String fileName, boolean matchCase)
      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

      public static int findFilesInDirectory(String dir, ArrayList<String> fileNames, boolean matchCase)
      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

      public static int findFilesInDirectory(File dir, ArrayList<String> fileNames, boolean matchCase)
      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

      public static String getFileTimestamp(String fileName)
      Get file time stamp as DayName Month day HH:MM:SS year.
    • getFileTimestamp

      public String getFileTimestamp()
      Get file time stamp as DayName Month day HH:MM:SS year.
    • getFileTimestamp

      public static String getFileTimestamp(long lastModified)
      Get file time stamp as DayName Month day HH:MM:SS year.
    • getBaseName

      public static String getBaseName(String fn)
      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

      public static boolean isSameFile(String fn1, String fn2)
      Checks if two file names refer to the same file, i.e. has the same canonical file names.
    • isSameFile

      public static boolean isSameFile(File f1, File f2)
      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

      public static String getRelativePath(File home, File f) throws IOException
      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

      public static String getRelativeFileName(String fileName, String reference)
      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

      public static boolean listDirectory(String dir, ArrayList<File> array)
      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

      public static boolean listDirectory(String dir, ArrayList<File> array, boolean includeDirectories)
      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

      public static boolean listDirectory(File dir, ArrayList<File> array)
      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

      public static boolean listDirectory(File dir, ArrayList<File> array, boolean includeDirectories)
      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

      public static boolean deleteFileAndChildren(File dir)
      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

      public static String getFileExtension(File file)
      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

      public static String getFileExtension(String fileName)
      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

      public 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. 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

      public static boolean isJarOrZipFile(String fileName)
      Checks if a file is of Jar or Zip file type (i.e. has JAR or ZIP file extension, case insensitive).
    • isJarOrZipFile

      public static boolean isJarOrZipFile(File file)
      Checks if a file is of Jar or Zip file type (i.e. has JAR or ZIP file extension, case insensitive).