138 lines
4.0 KiB
Plaintext
138 lines
4.0 KiB
Plaintext
|
package com.novelbook.android.utils;
|
||
|
|
||
|
import android.content.Context;
|
||
|
import android.os.Environment;
|
||
|
|
||
|
import org.mozilla.universalchardet.UniversalDetector;
|
||
|
|
||
|
import java.io.File;
|
||
|
import java.io.FileInputStream;
|
||
|
import java.io.IOException;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
|
||
|
|
||
|
public class FileUtils {
|
||
|
|
||
|
/**
|
||
|
* 获取文件编码
|
||
|
* @param fileName
|
||
|
* @return
|
||
|
* @throws IOException
|
||
|
*/
|
||
|
public static String getCharset(String fileName) throws IOException{
|
||
|
String charset;
|
||
|
FileInputStream fis = new FileInputStream(fileName);
|
||
|
byte[] buf = new byte[4096];
|
||
|
// (1)
|
||
|
UniversalDetector detector = new UniversalDetector(null);
|
||
|
// (2)
|
||
|
int nread;
|
||
|
while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {
|
||
|
detector.handleData(buf, 0, nread);
|
||
|
}
|
||
|
// (3)
|
||
|
detector.dataEnd();
|
||
|
// (4)
|
||
|
charset = detector.getDetectedCharset();
|
||
|
// (5)
|
||
|
detector.reset();
|
||
|
return charset;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 根据路径获取文件名
|
||
|
* @param pathandname
|
||
|
* @return
|
||
|
*/
|
||
|
public static String getFileName(String pathandname){
|
||
|
int start=pathandname.lastIndexOf("/");
|
||
|
int end=pathandname.lastIndexOf(".");
|
||
|
if(start!=-1 && end!=-1){
|
||
|
return pathandname.substring(start+1,end);
|
||
|
}else{
|
||
|
return "";
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
public static List<File> getSuffixFile(String filePath, String suffere){
|
||
|
List<File> files = new ArrayList<>();
|
||
|
File f = new File(filePath);
|
||
|
return getSuffixFile(files,f,suffere);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 读取sd卡上指定后缀的所有文件
|
||
|
* @param files 返回的所有文件
|
||
|
* @param f 路径(可传入sd卡路径)
|
||
|
* @param suffere 后缀名称 比如 .gif
|
||
|
* @return
|
||
|
*/
|
||
|
public static List<File> getSuffixFile(List<File> files, File f, final String suffere) {
|
||
|
if (!f.exists()) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
File[] subFiles = f.listFiles();
|
||
|
for (File subFile : subFiles) {
|
||
|
if (subFile.isHidden()){
|
||
|
continue;
|
||
|
}
|
||
|
if(subFile.isDirectory()){
|
||
|
getSuffixFile(files, subFile, suffere);
|
||
|
}else if(subFile.getName().endsWith(suffere)){
|
||
|
files.add(subFile);
|
||
|
} else{
|
||
|
//非指定目录文件 不做处理
|
||
|
}
|
||
|
// Log.e("filename",subFile.getName());
|
||
|
}
|
||
|
return files;
|
||
|
}
|
||
|
|
||
|
public static String getDiskCacheDir(Context context) {
|
||
|
String cachePath = null;
|
||
|
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|
||
|
|| !Environment.isExternalStorageRemovable()) {
|
||
|
cachePath = context.getExternalCacheDir().getPath();
|
||
|
} else {
|
||
|
cachePath = context.getCacheDir().getPath();
|
||
|
}
|
||
|
return cachePath;
|
||
|
}
|
||
|
public static File getNovelDir(int noveId){
|
||
|
File f = new File(BookUtil.chapterPath+noveId);
|
||
|
return f;
|
||
|
}
|
||
|
public static void clearChapterCache(int novelId) {
|
||
|
|
||
|
Fileutil.deleteDir(getNovelDir(novelId));
|
||
|
}
|
||
|
public static String getCacheSizeK(int noveId) {
|
||
|
float size = getCacheSize( noveId) ;
|
||
|
size = size/1024.00f;
|
||
|
if(size >1024){
|
||
|
size = size/1024.00f;
|
||
|
return String.format("%.1fM",size);
|
||
|
}
|
||
|
return String.format("%.1fK",size);
|
||
|
|
||
|
}
|
||
|
public static long getCacheSize(int noveId) {
|
||
|
return Fileutil.getDirSize(getNovelDir(noveId));
|
||
|
}
|
||
|
public static String getFormatedCachedSize(){
|
||
|
File f = new File(BookUtil.chapterPath);
|
||
|
return Fileutil.formatFileSize(Fileutil.getDirSize(f));
|
||
|
}
|
||
|
public static long getCachedSize2(){
|
||
|
File f = new File(BookUtil.chapterPath);
|
||
|
return Fileutil.getDirSize(f) ;
|
||
|
}
|
||
|
public static void clearCache(){
|
||
|
File f = new File(BookUtil.chapterPath);
|
||
|
Fileutil.deleteDir(f);
|
||
|
}
|
||
|
}
|