Class MFile


  • public class MFile
    extends Object
    Author:
    hummel
    • Constructor Detail

      • MFile

        public MFile()
    • Method Detail

      • setUnixPermissions

        public static boolean setUnixPermissions​(File file,
                                                 String permissionsStr)
        Set unix like permissions to a file. Only possible in unix like systems. Check MSystem.isWindows
        Parameters:
        file - The file
        permissionsStr - The posix string "rwxrwxrwx"
        Returns:
        true if the action was successful
      • getUnixPermissions

        public static String getUnixPermissions​(File file)
        Return a posix permission string. Only possible in unix like systems. Check MSystem.isWindows
        Parameters:
        file - The file
        Returns:
        The posix string like "rwxrwxrwx" or "rw-r--r--" or null if not possible
      • getFileSuffix

        public static String getFileSuffix​(File _file)
        Return the Suffix of a file. Its the string after the last dot.
        Parameters:
        _file -
        Returns:
        the file suffix
      • getFileSuffix

        public static String getFileSuffix​(String name)
        Return the Suffix of a file. Its the string after the last dot or an empty string.
        Parameters:
        name -
        Returns:
        the file suffix
      • getFileName

        public static String getFileName​(String path)
        Returns the name of the file in a path name. Using the OS specific separator. '/dir/subdir/file.ext' will return 'file.ext'. This function use the system separator slash or backslash for windows. If you always have a slash as directory separator use MUri.getFileName()
        Parameters:
        path -
        Returns:
        the file name
      • getFileDirectory

        public static String getFileDirectory​(String path)
        Returns the directory without file name or current directory. '/dir/subdir/file' will return '/dir/subdir'. This function use the system separator slash or backslash for windows. If you always have a slash as directory separator use MUri.getFileDirectory()
        Parameters:
        path -
        Returns:
        The previous directory name or null
      • getWorkingDirectory

        public static File getWorkingDirectory()
        return the internal working directory.
        Returns:
        current directory
      • readFile

        public static String readFile​(File _f)
        Open and read a file. It returns the content of the file as string.
        Parameters:
        _f -
        Returns:
        file content
      • readFile

        public static String readFile​(File _f,
                                      String encoding)
        Open and read a file. It returns the content of the file as string.
        Parameters:
        _f -
        encoding -
        Returns:
        file content
      • readFile

        public static String readFile​(Reader _is)
        Open and read a file. It returns the content of the file as string.
        Parameters:
        _is -
        Returns:
        file content
      • readFile

        public static String readFile​(InputStream _is)
        Open and read a stream. It returns the content of the file as string. Be aware of special characters.
        Parameters:
        _is -
        Returns:
        file content
      • readBinaryFile

        public static byte[] readBinaryFile​(File in)
                                     throws IOException
        Open and read a file. It returns the content of the file as byte array.
        Parameters:
        in -
        Returns:
        file content
        Throws:
        IOException
      • readBinary

        public static byte[] readBinary​(InputStream is)
                                 throws IOException
        Open and read a stream. It returns the content of the file as byte array.
        Parameters:
        is -
        Returns:
        file content
        Throws:
        IOException
      • readBinary

        public static byte[] readBinary​(InputStream is,
                                        boolean close)
                                 throws IOException
        Open and read a stream. It returns the content of the file as byte array.
        Parameters:
        is -
        close -
        Returns:
        file content
        Throws:
        IOException
      • readBinary

        public static void readBinary​(InputStream is,
                                      byte[] buffer,
                                      int offset,
                                      int length)
                               throws IOException
        Open and read a stream. It put the content of the file into the byte array.
        Parameters:
        is -
        buffer -
        offset -
        length -
        Throws:
        IOException
      • readBinary

        public static void readBinary​(ObjectInput is,
                                      byte[] buffer,
                                      int offset,
                                      int length)
                               throws IOException
        Open and read a stream. It put the content of the file into the byte array.
        Parameters:
        is -
        buffer -
        offset -
        length -
        Throws:
        IOException
      • writeFile

        public static boolean writeFile​(File _f,
                                        String _content)
        Open and write a file. Be aware of special characters.
        Parameters:
        _f -
        _content -
        Returns:
        true if successful
      • writeFile

        public static boolean writeFile​(File _f,
                                        byte[] _content)
        Open and write the content of the byte array a file.
        Parameters:
        _f -
        _content -
        Returns:
        true if successful
      • writeFile

        public static void writeFile​(OutputStream fos,
                                     byte[] _content,
                                     int offset,
                                     int length)
                              throws IOException
        Write the byte array to the stream.
        Parameters:
        fos -
        _content -
        offset -
        length -
        Throws:
        IOException
      • copyFile

        public static boolean copyFile​(File _src,
                                       File _dest)
        Copy a file.
        Parameters:
        _src -
        _dest -
        Returns:
        true if successful
      • copyFile

        public static void copyFile​(InputStream _is,
                                    OutputStream _os)
        Copy a stream.
        Parameters:
        _is -
        _os -
      • copyFile

        public static void copyFile​(Reader _is,
                                    Writer _os)
        Copy a stream.
        Parameters:
        _is -
        _os -
      • toFileName

        public static String toFileName​(String _name)
        Normalize the filename, removes all special characters.
        Parameters:
        _name -
        Returns:
        useful file name
      • deleteDir

        public static void deleteDir​(File _dir)
        Remove a directory and all included content.
        Parameters:
        _dir -
      • copyDir

        public static void copyDir​(File _src,
                                   File _dest)
        Copy a hole directory to another location.
        Parameters:
        _src -
        _dest -
      • copyDir

        public static void copyDir​(File _src,
                                   File _dest,
                                   String _filter)
        Copy a directory with content. Use the filter to filter the content.
        Parameters:
        _src -
        _dest -
        _filter -
      • copyDir

        public static void copyDir​(File _src,
                                   File _dest,
                                   String[] _filter)
      • normalize

        public static String normalize​(String name)
        Return a name free from problematic characters like slash, they will be changed to underscore
        Parameters:
        name -
        Returns:
        useful file name
      • normalizePath

        public static String normalizePath​(String name)
        Return a name free from problematic characters like back slash, they will be changed to underscore. The slash as separator between folders is allowed.
        Parameters:
        name -
        Returns:
        useful path
      • getFileNameOnly

        public static String getFileNameOnly​(String key)
        Returns the name without path and extension.
        Parameters:
        key -
        Returns:
        file name
      • replaceExtension

        public static String replaceExtension​(String name,
                                              String newExtension)
        Replace the Extension of the file
        Parameters:
        name -
        newExtension -
        Returns:
        the new file name
      • getMimeType

        public static String getMimeType​(String extension)
        Searching for the mime type in config and as last option have a static list of extensions.
        Parameters:
        extension - full file name or only extension
        Returns:
        the mime type
      • getMimeType

        public static String getMimeType​(String extension,
                                         String def)
        Searching for the mime type in config and as last option have a static list of extensions.
        Parameters:
        extension - full file name or only extension
        def -
        Returns:
        the mime type
      • loadMimeTypes

        public static void loadMimeTypes()
      • writeLines

        public static void writeLines​(File file,
                                      List<String> lines,
                                      boolean append)
                               throws IOException
        Write a line list to a file. Be aware of the ENTER \\n character in the lines ! Every line will be truncated by a ENTER \\n sign. This means the last line is empty.
        Parameters:
        file -
        lines -
        append -
        Throws:
        IOException
      • readLines

        public static List<String> readLines​(File file,
                                             boolean removeLastEmpty)
                                      throws IOException
        Read a file into a list line by line
        Parameters:
        file -
        removeLastEmpty - If you have written line by line the last ENTER will produce an empty line, set true to remove this line.
        Returns:
        the file content as list of lines
        Throws:
        IOException
      • readLines

        public static List<String> readLines​(InputStream is,
                                             boolean removeLastEmpty)
                                      throws IOException
        Read an stream into a list line by line. The stream will not be closed.
        Parameters:
        is -
        removeLastEmpty - If you have written line by line the last ENTER will produce an empty line, set true to remove this line.
        Returns:
        the file content as list of lines
        Throws:
        IOException
      • getUserHome

        public static File getUserHome()
      • releaseLock

        public static void releaseLock​(FileLock lock)
      • findAllFiles

        public static void findAllFiles​(File root,
                                        FileFilter fileFilter,
                                        List<File> list,
                                        int maxLevels)