public class FileHelper
extends java.lang.Object
限定符和类型 | 字段和说明 |
---|---|
static long |
MIN_AVAILABLE_SPACE_BYTES
最小可用空间 50M
|
构造器和说明 |
---|
FileHelper() |
限定符和类型 | 方法和说明 |
---|---|
static int |
copy(java.io.InputStream input,
java.io.OutputStream output)
Copy bytes from an
InputStream to an
OutputStream . |
static boolean |
copyFile(java.io.File fromFile,
java.io.File toFile)
复制文件
|
static long |
copyLarge(java.io.InputStream input,
java.io.OutputStream output,
byte[] buffer)
Copy bytes from a large (over 2GB)
InputStream to an
OutputStream . |
static void |
delete(java.io.File file)
遍历删除文件或文件夹
|
static void |
deleteSubs(java.io.File file)
删除子文件夹以及文件
|
static java.io.File |
getAppCacheDir(android.content.Context context,
boolean useSystem)
获取应用缓存目录
|
static java.io.File |
getAppCacheDir(android.content.Context context,
java.lang.String path,
boolean useSystem)
获取应用缓存目录
|
static long |
getAvailableStore(java.lang.String filePath)
* 获取存储卡的剩余容量,单位为字节
* @param filePath
* @return availableSpare
|
static byte[] |
getBytesFromFile(java.io.File file)
文件转化为字节数组
|
static byte[] |
getBytesFromFile(java.io.File file,
int length)
读取文件字节
|
static byte[] |
getBytesFromObject(java.io.Serializable obj)
从对象获取一个字节数组
|
static java.io.File |
getExternalStoragePublicDirectory(java.lang.String type)
获取公共外部存储目录
|
static java.lang.String[] |
getExternalStorages(android.content.Context context)
获取外部存储路径列表
/storage/emulated/0
/storage/extSdCard
/storage/UsbDriveA
/storage/UsbDriveB
/storage/UsbDriveC
/storage/UsbDriveD
/storage/UsbDriveE
/storage/UsbDriveF
|
static java.io.FileInputStream |
getFileInputStream(java.io.File file)
获取文件输入流
|
static java.lang.Object |
getObjectFromBytes(byte[] objBytes)
从字节数组获取对象
|
static boolean |
hasAvailableExternal(android.content.Context context)
扩展卡是否还有存储空间 (默认: 50M)
|
static java.lang.String |
md5sum(byte[] buffer)
返回数据MD5
|
static java.lang.String |
md5sum(java.io.File file)
返回文件的md5哈希值
|
static java.lang.String |
md5sum(java.io.InputStream inputStream)
返回文件的md5哈希值
|
static java.lang.String |
md5sum(java.lang.String filename)
返回文件的md5哈希值
|
static boolean |
mountedExternalStorage()
是否挂载了存储器
|
static byte[] |
readFile(java.io.File file)
读取文件流
|
static byte[] |
readFile(java.io.File file,
long start)
读取文件流
|
static byte[] |
readFile(java.io.File file,
long start,
long end)
读取文件字节
|
static boolean |
rename(java.io.File file,
java.io.File newPath)
重命名文件
|
static boolean |
safeClose(java.io.Closeable stream)
安全关闭输入流
|
static java.io.File |
saveFile(java.io.File file,
byte[] b)
把字节数组保存为一个文件
|
static boolean |
saveFile(java.io.File file,
java.io.InputStream stream)
保存图片
|
static java.io.File |
saveFile(java.lang.String filePath,
byte[] b)
把字节数组保存为一个文件
|
static java.lang.String |
toHexString(byte[] b)
提取MD5
|
public static final long MIN_AVAILABLE_SPACE_BYTES
public static java.lang.String toHexString(byte[] b)
b
- public static java.lang.String md5sum(java.io.File file)
file
- 文件对象public static java.lang.String md5sum(java.lang.String filename)
filename
- 文件pathpublic static java.lang.String md5sum(java.io.InputStream inputStream)
inputStream
- 输入流public static java.lang.String md5sum(byte[] buffer)
buffer
- public static void deleteSubs(java.io.File file)
file
- public static void delete(java.io.File file)
file
- public static byte[] readFile(java.io.File file)
file
- start
- public static byte[] readFile(java.io.File file, long start)
file
- start
- public static byte[] readFile(java.io.File file, long start, long end)
file
- start
- end
- public static boolean safeClose(java.io.Closeable stream)
stream
- public static int copy(java.io.InputStream input, java.io.OutputStream output) throws java.io.IOException
InputStream
to an
OutputStream
.
This method buffers the input internally, so there is no need to use a
BufferedInputStream
.
Large streams (over 2GB) will return a bytes copied value of
-1
after the copy has completed since the correct
number of bytes cannot be returned as an int. For large streams
use the copyLarge(InputStream, OutputStream)
method.
input
- the InputStream
to read fromoutput
- the OutputStream
to write tojava.lang.NullPointerException
- if the input or output is nulljava.io.IOException
- if an I/O error occurspublic static long copyLarge(java.io.InputStream input, java.io.OutputStream output, byte[] buffer) throws java.io.IOException
InputStream
to an
OutputStream
.
This method uses the provided buffer, so there is no need to use a
BufferedInputStream
.
input
- the InputStream
to read fromoutput
- the OutputStream
to write tobuffer
- the buffer to use for the copyjava.lang.NullPointerException
- if the input or output is nulljava.io.IOException
- if an I/O error occurspublic static java.io.FileInputStream getFileInputStream(java.io.File file)
file
- 文件public static boolean copyFile(java.io.File fromFile, java.io.File toFile)
fromFile
- toFile
- public static byte[] getBytesFromFile(java.io.File file)
file
- public static byte[] getBytesFromFile(java.io.File file, int length)
file
- length
- public static java.lang.Object getObjectFromBytes(byte[] objBytes) throws java.lang.Exception
objBytes
- java.lang.Exception
public static byte[] getBytesFromObject(java.io.Serializable obj) throws java.lang.Exception
obj
- java.lang.Exception
public static boolean rename(java.io.File file, java.io.File newPath)
file
- newPath
- public static java.io.File saveFile(java.lang.String filePath, byte[] b)
filePath
- b
- public static java.io.File saveFile(java.io.File file, byte[] b)
file
- b
- public static boolean saveFile(java.io.File file, java.io.InputStream stream)
file
- stream
- 输出文件流compress
- 0-100 如果设置为0 将保存为PNG格式public static boolean mountedExternalStorage()
public static java.io.File getExternalStoragePublicDirectory(java.lang.String type)
type
- The type of storage directory to return. Should be one of
#DIRECTORY_MUSIC
, #DIRECTORY_PODCASTS
,
#DIRECTORY_RINGTONES
, #DIRECTORY_ALARMS
,
#DIRECTORY_NOTIFICATIONS
, #DIRECTORY_PICTURES
,
#DIRECTORY_MOVIES
, #DIRECTORY_DOWNLOADS
, or
#DIRECTORY_DCIM
. May not be null.File.mkdirs()
.public static java.lang.String[] getExternalStorages(android.content.Context context)
context
- public static java.io.File getAppCacheDir(android.content.Context context, boolean useSystem)
context
- 内容上下文useSystem
- 是否使用系统目录(默认使用外部存储:SD card)public static java.io.File getAppCacheDir(android.content.Context context, java.lang.String path, boolean useSystem)
context
- 内容上下文path
- 子目录useSystem
- 是否使用系统目录(默认使用外部存储:SD card)public static boolean hasAvailableExternal(android.content.Context context)
context
- public static long getAvailableStore(java.lang.String filePath)