整理下项目中遇到的关于file的一些操作
目录
一、读取url到本地
/**
* 读取url到本地
* @param ossUrl 远程url地址
* @param savePath 保存在本地的路径
* @return 返回本地的绝对路径
*/
public static String readUrlToLocal(String ossUrl, String savePath) {
URL url;
try {
url = new URL(ossUrl);
String fileName = "/home/" + System.currentTimeMillis() + ossUrl.substring(ossUrl.lastIndexOf("/") + 1);
File file = new File(fileName);
FileUtils.copyURLToFile(url, file);
log.info("读取{}文件,存储地址为:{}", ossUrl, fileName);
return file.getAbsolutePath();
} catch (Exception e) {
throw new CloudProfileException("读取文件到本地失败!" + e.getMessage());
}
return null;
}
二、解压文件
/**
* 解压zip包
* @param zipPath 需要解压的zip包路径
* @param unZipPath 解压后的文件路径
* @return 返回解压后的file对象
*/
public static File unZip(String zipPath, String unZipPath) {
try {
return ZipUtil.unzip(zipPath, unZipPath);
} catch (Exception e) {
throw new CloudProfileException("解压zip包到本地失败:" + e.getMessage());
}
return null;
}
三、压缩文件
/**
* 压缩文件
* @param successFile 需要压缩的文件路径
* @param zipFilePath 压缩后的文件路径
*/
public static void zip(String successFile, String zipFilePath) {
File file = new File(successFile);
ZipEntry zipEntry;
try (
FileInputStream in = new FileInputStream(file);
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(zipFilePath))
) {
zipEntry = new ZipEntry(file.getName());
zip.putNextEntry(zipEntry);
byte[] bytes = new byte[1024];
int len;
while ((len = in.read(bytes)) != -1) {
zip.write(bytes, 0, len);
}
//删除本地的file文件
file.deleteOnExit();
} catch (Exception e) {
throw new CloudProfileException("压缩文件失败:" + e.getMessage());
}
}
四、list集合写入txt文件
/**
* 将list集合写入txt文件
* @param list list对象
* @param path 返回写入后的txt文件路径
*/
public static void readListToLocalTxt(List<String> list, String path) {
File file = new File(path);
try (
//将函数写到本地txt文件中
FileWriter writer = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(writer)
) {
for (String tProfileFunction : list) {
bufferedWriter.write(tProfileFunction);
bufferedWriter.newLine();
}
log.info("函数写入txt文件成功:" + path);
} catch (Exception e) {
throw new CloudProfileException("将函数写入txt文件失败:" + e.getMessage());
}
}
五、按行读取文件内容
/**
* 按行读取本地文件内容
* @param filePath 文件的本地路径 C:\Users\jie10.gao\Desktop\test.txt
*/
public static void readFile(String filePath) {
try(
FileInputStream inputStream = new FileInputStream(filePath);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
) {
String str = null;
while ((str = bufferedReader.readLine()) != null) {
System.out.println(str);
}
} catch (Exception e) {
log.error("读取文件失败:" + e.getMessage());
}
}
六、按照字节为单位读取文件的全部内容
/**
* 按照字节为单位读取文件的全部内容
* @param inputStream
* @return
*/
private static String readFile(InputStream inputStream) {
try {
byte[] buffer = new byte[1024]; // 1KB
StringBuilder stringBuffer = new StringBuilder();
int readSize = 0;
while ((readSize = inputStream.read(buffer)) != -1) {
String str = new String(buffer, 0, readSize, StandardCharsets.UTF_8);
stringBuffer.append(str);
}
inputStream.close();
return stringBuffer.toString();
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
七、删除某个文件
/**
* 删除某个文件
* @param localPath 文件的本地路径
*/
public static void deleteFile(String localPath) {
File file = new File(localPath);
boolean delete = file.delete();
if (!delete) {
throw new CloudProfileException("压缩包删除失败!");
}
}
八、递归删除某个非空文件夹
/**
* 递归删除非空文件夹
* @param file 文件夹的file对象
*/
public static void deleteFiles(File file) {
if (file.isFile()) {
boolean delete = file.delete();
if (!delete) {
throw new CloudProfileException("文件删除失败:" + file.getName());
}
} else {
File[] files = file.listFiles();
if (files != null) {
for (File file1 : files) {
deleteFiles(file1);
}
}
boolean delete = file.delete();
if (!delete) {
throw new CloudProfileException("空文件夹删除失败:" + file.getName());
}
}
}