mirror of
https://github.com/Geniusay/ChopperBot.git
synced 2026-05-31 11:01:15 +08:00
弹幕爬取模块第一版完成
This commit is contained in:
@@ -15,9 +15,6 @@ import java.io.IOException;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 文件工具类
|
||||
@@ -25,28 +22,31 @@ import java.util.List;
|
||||
public class FileUtil {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(FileUtil.class);
|
||||
|
||||
/**
|
||||
* 判断文件是否存在
|
||||
* @param dir 文件路径需要包含文件名
|
||||
* @return Boolean
|
||||
* 判断文件是否存在
|
||||
*
|
||||
* @param dir 文件路径需要包含文件名
|
||||
* @return Boolean
|
||||
*/
|
||||
public static Boolean isFileExist(String dir){
|
||||
public static Boolean isFileExist(String dir) {
|
||||
return new File(dir).exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制文件 比 Files更快
|
||||
* @param srcPath 源文件路径
|
||||
* @param destPath 目标文件路径
|
||||
* @return File
|
||||
* 复制文件 比 Files更快
|
||||
*
|
||||
* @param srcPath 源文件路径
|
||||
* @param destPath 目标文件路径
|
||||
* @return File
|
||||
*/
|
||||
public static File copyFile(String srcPath, String destPath) {
|
||||
try(
|
||||
try (
|
||||
FileChannel src = new FileInputStream(srcPath).getChannel();
|
||||
FileChannel dest = new FileInputStream(destPath).getChannel()
|
||||
){
|
||||
) {
|
||||
dest.transferFrom(src, 0, src.size());
|
||||
}catch (IOException e){
|
||||
} catch (IOException e) {
|
||||
logger.error("复制文件失败", e);
|
||||
return null;
|
||||
}
|
||||
@@ -55,21 +55,23 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param path 文件路径
|
||||
*
|
||||
* @param path 文件路径
|
||||
* @param filename 文件名
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean deleteFile(String path,String filename){
|
||||
return deleteFile(Paths.get(path,filename).toString());
|
||||
public static boolean deleteFile(String path, String filename) {
|
||||
return deleteFile(Paths.get(path, filename).toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
*
|
||||
* @param path 文件路径
|
||||
* @return boolean
|
||||
* @throws IOException IOException
|
||||
*/
|
||||
public static boolean deleteFile(String path){
|
||||
public static boolean deleteFile(String path) {
|
||||
try {
|
||||
Files.delete(Paths.get(path));
|
||||
} catch (IOException e) {
|
||||
@@ -81,39 +83,42 @@ public class FileUtil {
|
||||
|
||||
/**
|
||||
* 文件目录递归删除
|
||||
*
|
||||
* @param path 文件路径
|
||||
* @return boolean
|
||||
* @throws IOException IOException
|
||||
* @throws IOException IOException
|
||||
*/
|
||||
public static boolean deleteDirectory(String path) throws IOException {
|
||||
FileCondition condition = file -> !file.toString().startsWith("C:")||!file.toString().startsWith("root");
|
||||
FileCondition condition = file -> !file.toString().startsWith("C:") || !file.toString().startsWith("root");
|
||||
return deleteDirectory0(path, condition, condition, condition);
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件目录递归删除
|
||||
* @param path 文件路径
|
||||
* @param visit 访问文件时触发该方法
|
||||
* @param preVisit 访问子目录前触发该方法
|
||||
* 文件目录递归删除
|
||||
*
|
||||
* @param path 文件路径
|
||||
* @param visit 访问文件时触发该方法
|
||||
* @param preVisit 访问子目录前触发该方法
|
||||
* @param postVisit 访问目录之后触发该方法
|
||||
* @return boolean
|
||||
* @throws IOException IOException
|
||||
*/
|
||||
public static boolean deleteDirectory(String path,FileCondition visit, FileCondition preVisit, FileCondition postVisit) throws IOException {
|
||||
public static boolean deleteDirectory(String path, FileCondition visit, FileCondition preVisit, FileCondition postVisit) throws IOException {
|
||||
return deleteDirectory0(path, visit, preVisit, postVisit);
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归删除
|
||||
* @param path 文件路径
|
||||
* @return File
|
||||
* 递归删除
|
||||
*
|
||||
* @param path 文件路径
|
||||
* @return File
|
||||
*/
|
||||
private static boolean deleteDirectory0(String path, FileCondition visit, FileCondition preVisit, FileCondition postVisit) throws IOException {
|
||||
Files.walkFileTree(Paths.get(path), new SimpleFileVisitor<Path>() {
|
||||
// 在访问文件时触发该方法
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||
if(!visit.condition(file)){
|
||||
if (!visit.condition(file)) {
|
||||
logger.info("文件被跳过: {}", file);
|
||||
return FileVisitResult.SKIP_SUBTREE;
|
||||
}
|
||||
@@ -125,7 +130,7 @@ public class FileUtil {
|
||||
// 在访问子目录前触发该方法
|
||||
@Override
|
||||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
||||
if(!preVisit.condition(dir)){
|
||||
if (!preVisit.condition(dir)) {
|
||||
logger.info("目录被跳过: {}", dir);
|
||||
return FileVisitResult.SKIP_SUBTREE;
|
||||
}
|
||||
@@ -136,7 +141,7 @@ public class FileUtil {
|
||||
// 在访问目录之后触发该方法
|
||||
@Override
|
||||
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
||||
if(!postVisit.condition(dir)){
|
||||
if (!postVisit.condition(dir)) {
|
||||
logger.info("目录被跳过: {}", dir);
|
||||
return FileVisitResult.SKIP_SUBTREE;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user