public final class FileUtility extends Object
Title: 常用代码打包
Description: This class provides basic facilities for manipulating files and file paths.
Methods exist to retrieve the components of a typical file path. For example
/www/hosted/mysite/index.html, can be broken into:
/www/hosted/mysite/ -- retrievable through getPath(java.lang.String)index.html -- retrievable through removePath(java.lang.String)/www/hosted/mysite/index -- retrievable through removeExtension(java.lang.String)html -- retrievable through getExtension(java.lang.String)concatenate two paths, resolve a
path relative to a File and normalize(java.lang.String) a path.
There are methods to create a File from a URL, copy a
File to a directory,
copy a File to another File,
copy a URL's contents to a File,
as well as methods to delete and clean a directory.
Copyright: Copyright (c) 2006
Company: www.justdos.net
| 限定符和类型 | 字段和说明 |
|---|---|
static int |
DISKFILE_DOC |
static int |
DISKFILE_JPG |
static int |
DISKFILE_MUSIC |
static int |
DISKFILE_OTHER
Private constructor to prevent instantiation.
|
static int |
DISKFILE_PDF |
static int |
DISKFILE_PIC |
static int |
DISKFILE_PNG |
static int |
DISKFILE_SWF |
static int |
DISKFILE_VIDEO |
static int |
DISKFILE_ZIP |
| 限定符和类型 | 方法和说明 |
|---|---|
static String |
catPath(String lookupPath,
String path)
Will concatenate 2 paths, dealing with
.. |
static int |
checkOfficeType(String oldfilepath)
检测文件类型,是否是 office文档
|
static int |
checkPictureType(String oldfilepath)
检测文件类型,是否是 picture
|
static int |
checkVideoFileType(String oldfilepath)
检测文件类型是否是视频文件
|
static void |
cleanDirectory(File directory)
Clean a directory without deleting it.
|
static void |
cleanDirectory(String directory)
Clean a directory without deleting it.
|
static boolean |
contentEquals(File file1,
File file2)
Compare the contents of two files to determine if they are equal or not.
|
static void |
copyFile(File source,
File destination)
Copy file from source to destination.
|
static void |
copyFileToDirectory(File source,
File destinationDirectory)
Copy file from source to destination.
|
static void |
copyFileToDirectory(String source,
String destinationDirectory)
Copy file from source to destination.
|
static void |
copyURLToFile(URL source,
File destination)
Copies bytes from the URL
source to a file destination. |
static void |
deleteDirectory(File directory)
Recursively delete a directory.
|
static void |
deleteDirectory(String directory)
Recursively delete a directory.
|
static void |
forceDelete(File file)
Delete a file.
|
static void |
forceDelete(String file)
Delete a file.
|
static String |
getExtension(String filename)
Get extension from filename.
|
static int |
getFileType(String filename)
根据文件后缀名得到文件类型
|
static String |
getPath(String filepath)
Get path from filename.
|
static String |
getPath(String filepath,
char fileSeparatorChar)
Get path from filename.
|
static File |
getUniqueFile(String dir,
String originFile)
Get an unique file
|
static File |
getUniquePath(String basePath,
String subPath)
Get an unique path file
|
static String |
normalize(String path)
Normalize a path.
|
static String |
removeExtension(String filename)
Remove extension from filename.
|
static String |
removeExtention(String filename)
已过时。
Use removeExtension as removeExtention is mispelled
|
static String |
removePath(String filepath)
Remove path from filename.
|
static String |
removePath(String filepath,
char fileSeparatorChar)
Remove path from filename.
|
static File |
resolveFile(File baseFile,
String filename)
Resolve a file
filename to it's canonical form. |
static long |
sizeOfDirectory(File directory)
Recursively count size of a directory.
|
static long |
sizeOfDirectory(String directory)
Recursively count size of a directory.
|
static File |
toFile(URL url)
Convert from a
URL to a File. |
static URL[] |
toURLs(File[] files)
Convert the array of Files into a list of URLs.
|
public static final int DISKFILE_OTHER
public static final int DISKFILE_DOC
public static final int DISKFILE_PIC
public static final int DISKFILE_MUSIC
public static final int DISKFILE_VIDEO
public static final int DISKFILE_ZIP
public static final int DISKFILE_SWF
public static final int DISKFILE_PDF
public static final int DISKFILE_JPG
public static final int DISKFILE_PNG
public static boolean contentEquals(File file1, File file2) throws IOException
file1 - the first filefile2 - the second fileIOExceptionpublic static File toFile(URL url)
URL to a File.url - File URL.File object, or null if the URL's protocol
is not filepublic static URL[] toURLs(File[] files) throws IOException
files - the array of filesIOException - if an error occurspublic static String removeExtention(String filename)
foo.txt --> foo a\b\c.jpg --> a\b\c a\b\c --> a\b\c
filename - the filenamepublic static String removeExtension(String filename)
foo.txt --> foo a\b\c.jpg --> a\b\c a\b\c --> a\b\c
filename - the filenamepublic static String getExtension(String filename)
foo.txt --> "txt" a\b\c.jpg --> "jpg" a\b\c --> ""
filename - the filenamepublic static String removePath(String filepath)
basename
ie.
a/b/c.txt --> c.txt a.txt --> a.txt
filepath - the filepathpublic static String removePath(String filepath, char fileSeparatorChar)
a/b/c.txt --> c.txt a.txt --> a.txt
filepath - the filepathpublic static String getPath(String filepath)
dirname.
ie.
a/b/c.txt --> a/b a.txt --> ""
filepath - the filepathpublic static int getFileType(String filename)
oldfilepath - public static int checkOfficeType(String oldfilepath)
oldfilepath - public static int checkPictureType(String oldfilepath)
oldfilepath - public static int checkVideoFileType(String oldfilepath)
oldfilepath - public static String getPath(String filepath, char fileSeparatorChar)
a/b/c.txt --> a/b a.txt --> ""
filepath - the filepathpublic static void copyFileToDirectory(String source, String destinationDirectory) throws IOException
destinationDirectory does not exist, it
(and any parent directories) will be created. If a file source in
destinationDirectory exists, it will be overwritten.source - An existing File to copy.destination - A directory to copy source into.FileNotFoundException - if source isn't a normal file.IllegalArgumentException - if destinationDirectory isn't a directory.IOException - if source does not exist, the file in
destinationDirectory cannot be written to, or an IO error occurs during copying.public static void copyFileToDirectory(File source, File destinationDirectory) throws IOException
destinationDirectory does not exist, it
(and any parent directories) will be created. If a file source in
destinationDirectory exists, it will be overwritten.source - An existing File to copy.destination - A directory to copy source into.FileNotFoundException - if source isn't a normal file.IllegalArgumentException - if destinationDirectory isn't a directory.IOException - if source does not exist, the file in
destinationDirectory cannot be written to, or an IO error occurs during copying.public static void copyFile(File source, File destination) throws IOException
destination will be
created if they don't already exist. destination will be overwritten if it
already exists.source - An existing non-directory File to copy bytes from.destination - A non-directory File to write bytes to (possibly
overwriting).IOException - if source does not exist, destination cannot be
written to, or an IO error occurs during copying.FileNotFoundException - if destination is a directory
(use copyFileToDirectory(java.lang.String, java.lang.String)).public static void copyURLToFile(URL source, File destination) throws IOException
source to a file destination.
The directories up to destination will be created if they don't already exist.
destination will be overwritten if it already exists.source - A URL to copy bytes from.destination - A non-directory File to write bytes to (possibly
overwriting).IOException - if
source URL cannot be openeddestination cannot be written topublic static String normalize(String path)
null if the ..'s went past the
root.
Eg:
/foo// --> /foo/ /foo/./ --> /foo/ /foo/../bar --> /bar /foo/../bar/ --> /bar/ /foo/../bar/../baz --> /baz //foo//./bar --> /foo/bar /../ --> null
path - the path to normalizenull if too many ..'s.public static String catPath(String lookupPath, String path)
..
Eg.,
/a/b/c + d = /a/b/d
/a/b/c + ../d = /a/d
public static File resolveFile(File baseFile, String filename)
filename to it's canonical form. If filename is
relative (doesn't start with /), it will be resolved relative to
baseFile, otherwise it is treated as a normal root-relative path.baseFile - Where to resolve filename from, if filename is
relative.filename - Absolute or relative file path to resolve.File of filename.public static void forceDelete(String file) throws IOException
IOExceptionpublic static void forceDelete(File file) throws IOException
IOExceptionpublic static void deleteDirectory(String directory) throws IOException
IOExceptionpublic static void deleteDirectory(File directory) throws IOException
IOExceptionpublic static void cleanDirectory(String directory) throws IOException
IOExceptionpublic static void cleanDirectory(File directory) throws IOException
IOExceptionpublic static long sizeOfDirectory(String directory)
public static long sizeOfDirectory(File directory)
public static File getUniquePath(String basePath, String subPath)
basePath - base pathsubPath - sub directory nameCopyright © 2023 onecode. All rights reserved.