webssh/src/main/java/net/educoder/bridge/common/utils/FileUtil.java

35 lines
1.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package net.educoder.bridge.common.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
/**
* 文件工具类
*
* @author 威少
*/
public class FileUtil {
private static Logger logger = LoggerFactory.getLogger(FileUtil.class);
/**
* 采用Files的delete方法不直接使用file.delete是因为file.delete在删除失败的场景下只会返回false没有具体的错误原因返回
*/
public static String delete(File file) {
String path = file.getPath();
try {
Files.delete(file.toPath());
logger.info("删除文件成功file: {}", path);
} catch (NoSuchFileException e) {
logger.info("删除文件失败file: {},文件不存在", path);
} catch (IOException e) {
logger.warn("删除文件失败file: {}e: {}", path, e.getMessage());
}
return path;
}
}