
io.afu.utils.files.FileUtils Maven / Gradle / Ivy
package io.afu.utils.files;
import io.afu.utils.exception.BaseException;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.rmi.server.ExportException;
public class FileUtils {
/**
* 获取带后缀的文件名
* @param filePath 文件路径
* @return String
*/
public static String getFileNameByFilePath(String filePath){
String fileName = null;
if (filePath.contains("\\")){
fileName =filePath.substring(filePath.lastIndexOf("\\")+1,filePath.length());
}else {
fileName = filePath.substring(filePath.lastIndexOf("/")+1,filePath.length());
}
return fileName;
}
/**
* 获取不带后缀的文件名
* @param filePath 文件路径
* @return String
*/
public static String getFileNameWithoutSuffix(String filePath) {
String nameWithoutSuffix = null;
String fileName = getFileNameByFilePath(filePath);
nameWithoutSuffix = fileName.substring(0,fileName.lastIndexOf("."));
return nameWithoutSuffix;
}
/**
* 通过URL来获取文件后缀
* @param url 文件url
* @return 后缀名
*/
public static String getFileNameByUrl(String url){
String[] strs = url.split("\\?");
String pureUrl = strs[0];
return pureUrl.substring(pureUrl.lastIndexOf("/")+1,pureUrl.length());
}
/**
* 获取文件的二进制代码
* @param filePath 文件路径
* @return 返回二进制数组
* @throws BaseException 文件读取出错抛错
*/
public static byte[] getBytesFromFilePath(String filePath) throws BaseException {
byte[] buffer = null;
try {
File file = new File(filePath);
FileInputStream fileInputStream = new FileInputStream(file);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n=fileInputStream.read(b))!=-1){
byteArrayOutputStream.write(b,0,n);
}
fileInputStream.close();
byteArrayOutputStream.close();
buffer = byteArrayOutputStream.toByteArray();
}catch (Exception e){
throw new BaseException(e);
}
return buffer;
}
/**
* 写入bytes 到文件
* @param filePath 文件路径
* @param bytes 字节数组
* @throws BaseException 抛错
*/
public static void writeBytesToFile(String filePath,byte[] bytes,Boolean add) throws BaseException {
try (FileOutputStream fileOutputStream = new FileOutputStream(filePath,add)){
fileOutputStream.write(bytes);
}catch (Exception e){
throw new BaseException(e);
}
}
/**
* 写入String
* @param filePath 路径
* @param toWrite 需要写入的String
* @throws BaseException 抛错
*/
public static void writeStringToFile(String filePath,String toWrite) throws BaseException {
byte[] toWriteArray = toWrite.getBytes();
writeBytesToFile(filePath,toWriteArray,false);
}
/**
* 写入String
* @param filePath 路径
* @param toWrite 需要写入的String
* @param add 是否追加到文件中
* @throws BaseException 抛错
*/
public static void writeStringToFile(String filePath,String toWrite,Boolean add) throws BaseException {
byte[] toWriteArray = toWrite.getBytes();
writeBytesToFile(filePath,toWriteArray,add);
}
/**
* 判断文件是否存在
* @param filePath 文件路径
* @return 是否存在的布尔值
* @throws BaseException 抛错
*/
public static Boolean exist(String filePath) throws BaseException {
File file = new File(filePath);
return file.exists();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy