mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-06-07 14:24:03 +08:00
优化fileUtil
This commit is contained in:
parent
c85efeb4a6
commit
a9f8c7aca3
@ -11,6 +11,7 @@ import java.nio.MappedByteBuffer;
|
|||||||
import java.nio.channels.FileChannel;
|
import java.nio.channels.FileChannel;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.logging.Logger;
|
||||||
import java.util.zip.CRC32;
|
import java.util.zip.CRC32;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -22,6 +23,8 @@ import java.util.zip.CRC32;
|
|||||||
* @time 2017年11月3日 下午3:12:49
|
* @time 2017年11月3日 下午3:12:49
|
||||||
*/
|
*/
|
||||||
public class FileUtil {
|
public class FileUtil {
|
||||||
|
static Logger logger = Logger.getLogger(FileUtil.class.getName());
|
||||||
|
|
||||||
private FileUtil() {
|
private FileUtil() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,15 +40,16 @@ public class FileUtil {
|
|||||||
* @param fileName
|
* @param fileName
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String getFileType(String fileName) throws FileNotFoundException {
|
public static String getFileType(String fileName) {
|
||||||
try {
|
try {
|
||||||
if (fileName.lastIndexOf(TYPE_TAR_GZ) > 0) {
|
if (fileName.lastIndexOf(TYPE_TAR_GZ) > 0) {
|
||||||
return TAR_GZ;
|
return TAR_GZ;
|
||||||
}
|
}
|
||||||
return fileName.substring(fileName.lastIndexOf(".") + 1);
|
return fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new FileNotFoundException("文件类型未能解析");
|
logger.severe("file doesn't exist or is not a file");
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -56,12 +60,13 @@ public class FileUtil {
|
|||||||
public static void judeFileExists(File file) {
|
public static void judeFileExists(File file) {
|
||||||
|
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
System.out.println("file exists");
|
logger.severe("file exists");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("file not exists, create it ...");
|
logger.info("file not exists, create it ...");
|
||||||
try {
|
try {
|
||||||
file.createNewFile();
|
file.createNewFile();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
logger.severe("file create fail");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,12 +89,12 @@ public class FileUtil {
|
|||||||
public static boolean judeDirExists(File file) {
|
public static boolean judeDirExists(File file) {
|
||||||
if (file.exists()) {
|
if (file.exists()) {
|
||||||
if (file.isDirectory()) {
|
if (file.isDirectory()) {
|
||||||
System.out.println("dir exists");
|
logger.severe("dir exists");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("the same name file exists, can not create dir");
|
logger.severe("the same name file exists, can not create dir");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
System.out.println("dir not exists, create it ...");
|
logger.info("dir not exists, create it ...");
|
||||||
return file.mkdirs();
|
return file.mkdirs();
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
@ -121,10 +126,13 @@ public class FileUtil {
|
|||||||
}
|
}
|
||||||
return sha1;
|
return sha1;
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
logger.severe("system algorithm error.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
|
logger.severe("file doesn't exist or is not a file");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
logger.severe("The operation file is an IO exception.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
@ -132,6 +140,7 @@ public class FileUtil {
|
|||||||
in.close();
|
in.close();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
logger.severe("close FileInputStream IO exception.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@ -154,10 +163,13 @@ public class FileUtil {
|
|||||||
messagedigest.update(byteBuffer);
|
messagedigest.update(byteBuffer);
|
||||||
return StrUtil.toHex(messagedigest.digest());
|
return StrUtil.toHex(messagedigest.digest());
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
logger.severe("system algorithm error.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
|
logger.severe("file doesn't exist or is not a file");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
logger.severe("The operation file is an IO exception.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@ -189,16 +201,16 @@ public class FileUtil {
|
|||||||
}
|
}
|
||||||
return md5;
|
return md5;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println(e);
|
logger.severe("The operation file is an IO exception.");
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
System.out.println(e);
|
logger.severe("system algorithm error.");
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
if (in != null) {
|
if (in != null) {
|
||||||
in.close();
|
in.close();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println(e);
|
logger.severe("close FileInputStream IO exception.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@ -222,10 +234,13 @@ public class FileUtil {
|
|||||||
messagedigest.update(byteBuffer);
|
messagedigest.update(byteBuffer);
|
||||||
return StrUtil.toHex(messagedigest.digest());
|
return StrUtil.toHex(messagedigest.digest());
|
||||||
} catch (NoSuchAlgorithmException e) {
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
logger.severe("system algorithm error.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
|
logger.severe("file doesn't exist or is not a file");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
logger.severe("The operation file is an IO exception.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@ -249,9 +264,11 @@ public class FileUtil {
|
|||||||
}
|
}
|
||||||
return crc32.getValue() + "";
|
return crc32.getValue() + "";
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
|
logger.severe("file doesn't exist or is not a file");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
logger.severe("The operation file is an IO exception.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
@ -260,6 +277,7 @@ public class FileUtil {
|
|||||||
fileInputStream.close();
|
fileInputStream.close();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
logger.severe("close FileInputStream IO exception.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -281,6 +299,7 @@ public class FileUtil {
|
|||||||
fileInputStream.read(data);
|
fileInputStream.read(data);
|
||||||
fileInputStream.close();
|
fileInputStream.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
logger.severe("The operation file is an IO exception.");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
// 对字节数组Base64编码
|
// 对字节数组Base64编码
|
||||||
@ -288,8 +307,66 @@ public class FileUtil {
|
|||||||
return encoder.encode(data);
|
return encoder.encode(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取文件大小 :直接返回大小
|
||||||
|
*
|
||||||
|
* @param f
|
||||||
|
* @return f.length()
|
||||||
|
*/
|
||||||
|
public static long size(File f) {
|
||||||
|
if (f.exists() && f.isFile()) {
|
||||||
|
return f.length();
|
||||||
|
} else {
|
||||||
|
logger.info("file doesn't exist or is not a file");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* public static void main(String[] args) {
|
/**
|
||||||
|
* 获取文件大小 : 用流的方式获取
|
||||||
|
*
|
||||||
|
* @param f
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static long size4Stream(File f) {
|
||||||
|
FileChannel fc = null;
|
||||||
|
try {
|
||||||
|
if (f.exists() && f.isFile()) {
|
||||||
|
FileInputStream fis = new FileInputStream(f);
|
||||||
|
fc = fis.getChannel();
|
||||||
|
return fc.size();
|
||||||
|
} else {
|
||||||
|
logger.info("file doesn't exist or is not a file");
|
||||||
|
}
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
logger.severe("file doesn't exist or is not a file");
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.severe("The operation file is an IO exception.");
|
||||||
|
} finally {
|
||||||
|
if (null != fc) {
|
||||||
|
try {
|
||||||
|
fc.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.severe("close FileInputStream IO exception.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*public static void main(String[] args) {
|
||||||
|
long size = FileUtil.size(new File("E:\\OS\\deepin-15.6-amd64\\DeepinCloudPrintServerInstaller_1.0.0.1.exe"));
|
||||||
|
System.out.println(size);
|
||||||
|
System.out.println(1024 * 1024 * 5);
|
||||||
|
if (size > 1024 * 1024 * 5) {
|
||||||
|
System.out.println("文件最大5M");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
long s1 = fileSize(new File("E:\\OS\\cn_windows_10_consumer_editions_version_1803_updated_march_2018_x64_dvd_12063766.iso"));
|
||||||
|
System.out.println(s1);
|
||||||
|
long s2 = fileSize4Stream(new File("E:\\OS\\cn_windows_10_consumer_editions_version_1803_updated_march_2018_x64_dvd_12063766.iso"));
|
||||||
|
System.out.println(s2);
|
||||||
|
|
||||||
String s1 = base64(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
|
String s1 = base64(new File("C:\\Users\\Administrator\\Desktop\\a.html"));
|
||||||
System.out.println(s1);
|
System.out.println(s1);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user