Package de.mhus.lib.core
Class MFile
- java.lang.Object
-
- de.mhus.lib.core.MFile
-
public class MFile extends Object
- Author:
- hummel
-
-
Field Summary
Fields Modifier and Type Field Description static StringDEFAULT_MIMEstatic StringTYPE_PDF
-
Constructor Summary
Constructors Constructor Description MFile()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static FileLockaquireLock(File lockFile, long timeout)static voidcopyDir(File _src, File _dest)Copy a hole directory to another location.static voidcopyDir(File _src, File _dest, FileFilter _filter)static voidcopyDir(File _src, File _dest, String _filter)Copy a directory with content.static voidcopyDir(File _src, File _dest, String[] _filter)static booleancopyFile(File _src, File _dest)Copy a file.static voidcopyFile(InputStream _is, OutputStream _os)Copy a stream.static voidcopyFile(Reader _is, Writer _os)Copy a stream.static voiddeleteDir(File _dir)Remove a directory and all included content.static List<File>findAllFiles(File root, FileFilter fileFilter)static voidfindAllFiles(File root, FileFilter fileFilter, List<File> list, int maxLevels)static FilegetFile(File parent, String path)static FileCheckergetFileCheck(String type)static StringgetFileDirectory(String path)Returns the directory without file name or current directory.static StringgetFileName(String path)Returns the name of the file in a path name.static StringgetFileNameOnly(String key)Returns the name without path and extension.static StringgetFileSuffix(File _file)Return the Suffix of a file.static StringgetFileSuffix(String name)Return the Suffix of a file.static StringgetMimeType(String extension)Searching for the mime type in config and as last option have a static list of extensions.static StringgetMimeType(String extension, String def)Searching for the mime type in config and as last option have a static list of extensions.static StringgetUnixPermissions(File file)Return a posix permission string.static FilegetUserHome()static FilegetWorkingDirectory()return the internal working directory.static voidloadMimeTypes()static Stringnormalize(String name)Return a name free from problematic characters like slash, they will be changed to underscorestatic StringnormalizePath(String name)Return a name free from problematic characters like back slash, they will be changed to underscore.static ReaderopenFileReader(File file, String encoding)static byte[]readBinary(InputStream is)Open and read a stream.static byte[]readBinary(InputStream is, boolean close)Open and read a stream.static voidreadBinary(InputStream is, byte[] buffer, int offset, int length)Open and read a stream.static voidreadBinary(ObjectInput is, byte[] buffer, int offset, int length)Open and read a stream.static byte[]readBinaryFile(File in)Open and read a file.static StringreadFile(File _f)Open and read a file.static StringreadFile(File _f, String encoding)Open and read a file.static StringreadFile(InputStream _is)Open and read a stream.static StringreadFile(InputStream _is, String encoding)static StringreadFile(Reader _is)Open and read a file.static List<String>readLines(File file, boolean removeLastEmpty)Read a file into a list line by linestatic voidreadLines(File file, IObserver<String> lineObserver)static List<String>readLines(InputStream is, boolean removeLastEmpty)Read an stream into a list line by line.static voidreadLines(InputStream is, IObserver<String> lineObserver)The stream will not be closed.static voidreadLines(Reader r, IObserver<String> lineObserver)static voidreleaseLock(FileLock lock)static StringreplaceExtension(String name, String newExtension)Replace the Extension of the filestatic StringreplaceSuffix(String name, String newSuffix)static booleansetUnixPermissions(File file, String permissionsStr)Set unix like permissions to a file.static StringtoFileName(String _name)Normalize the filename, removes all special characters.static booleanwriteFile(File _f, byte[] _content)Open and write the content of the byte array a file.static booleanwriteFile(File _f, String _content)Open and write a file.static voidwriteFile(OutputStream fos, byte[] _content, int offset, int length)Write the byte array to the stream.static booleanwriteFile(OutputStream os, String content)static booleanwriteFile(OutputStream os, String _content, String charsetName)static voidwriteLines(File file, List<String> lines, boolean append)Write a line list to a file.
-
-
-
Field Detail
-
DEFAULT_MIME
public static final String DEFAULT_MIME
- See Also:
- Constant Field Values
-
TYPE_PDF
public static final String TYPE_PDF
- See Also:
- Constant Field Values
-
-
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 filepermissionsStr- 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
-
readFile
public static String readFile(InputStream _is, String encoding)
-
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(OutputStream os, String content)
-
writeFile
public static boolean writeFile(OutputStream os, String _content, String charsetName)
-
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, FileFilter _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 extensiondef-- 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
-
readLines
public static void readLines(File file, IObserver<String> lineObserver) throws IOException
- Throws:
IOException
-
readLines
public static void readLines(InputStream is, IObserver<String> lineObserver) throws IOException
The stream will not be closed.- Parameters:
is-lineObserver-- Throws:
IOException
-
readLines
public static void readLines(Reader r, IObserver<String> lineObserver) throws IOException
- Throws:
IOException
-
getUserHome
public static File getUserHome()
-
releaseLock
public static void releaseLock(FileLock lock)
-
aquireLock
public static FileLock aquireLock(File lockFile, long timeout) throws IOException, TimeoutException
- Throws:
IOExceptionTimeoutException
-
getFileCheck
public static FileChecker getFileCheck(String type)
-
openFileReader
public static Reader openFileReader(File file, String encoding) throws FileNotFoundException, UnsupportedEncodingException
-
findAllFiles
public static List<File> findAllFiles(File root, FileFilter fileFilter)
-
findAllFiles
public static void findAllFiles(File root, FileFilter fileFilter, List<File> list, int maxLevels)
-
-