netty中文件的使用
1. 遍历文件夹和文件
/**
* 遍历文件夹和文件
*
* @throws IOException
*/
private static void m1() throws IOException {
// 计数器
AtomicInteger dirCount = new AtomicInteger();
AtomicInteger fileCount = new AtomicInteger();
Files.walkFileTree(Paths.get("E:\\ideaWork\\netty_study\\target"), new SimpleFileVisitor<Path>() {
/**
* 遍历文件夹
* 此方法在遍历目录之前使用
* @param dir
* @param attrs
* @return
* @throws IOException
*/
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("目录名称为==========》" + dir);
dirCount.incrementAndGet();
return super.preVisitDirectory(dir, attrs);
}
/**
* 遍历文件
* 此方法在遍历文件之前使用
* @param file
* @param attrs
* @return
* @throws IOException
*/
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("文件名称为==========》" + file);
fileCount.incrementAndGet();
return super.visitFile(file, attrs);
}
});
System.out.println("dir count:" + dirCount);
System.out.println("file count:" + fileCount);
}
2. 遍历某一类文件有多少个
/**
* 遍历某一类文件有多少个
* @throws IOException
*/
private static void m2() throws IOException {
AtomicInteger classCount = new AtomicInteger();
Files.walkFileTree(Paths.get("E:\\ideaWork\\netty_study\\target"), new SimpleFileVisitor<Path>() {
/**
* 遍历文件
* 此方法在遍历文件之前使用
* @param file
* @param attrs
* @return
* @throws IOException
*/
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toString().endsWith(".class")) {
System.out.println(file);
classCount.incrementAndGet();
}
return super.visitFile(file, attrs);
}
});
}
3. 删除多级目录
/**
* 删除多级目录 直接删除了 没有备份,回收站里面找不到
* @throws IOException
*/
private static void m3() throws IOException {
Files.walkFileTree(Paths.get("E:\\ideaWork\\netty_study\\target"), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return super.visitFile(file, attrs);
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return super.postVisitDirectory(dir, exc);
}
});
}
4.文件复制
/**
* 文件复制
* @throws IOException
*/
public static void m4() throws IOException {
// 源目录
String source = "E:\\ideaWork\\netty_study\\target\\classes";
// 目标目录
String target = "C:\\Users\\Administrator\\Desktop\\zzh";
Files.walk(Paths.get(source)).forEach(path -> {
try {
String targetName = path.toString().replace(source, target);
// 是目录
if (Files.isDirectory(path)) {
// 如果目录存在,报错
Files.createDirectory(Paths.get(targetName));
}
// 是文件
if (Files.isRegularFile(path)) {
Files.copy(path, Paths.get(targetName));
}
} catch (IOException e) {
e.printStackTrace();
}
});
}
5.文件传输
/**
* 两个文件之间的数据传输
* 效率高,底层会利用操作系统的零拷贝进行优化
*/
public static void m() {
try (
// 获取文件数据
FileChannel from = new FileInputStream("data.txt").getChannel();
// 写入文件数据
FileChannel to = new FileOutputStream("to.txt").getChannel()
) {
// 效率高,底层会利用操作系统的零拷贝进行优化,只能传输2g数据
// from.transferTo(0,from.size(),to);
long size = from.size();
// left 表示还剩下多少字节
for (long left = size; left > 0; ) {
left -= from.transferTo((size - left), left, to);
}
} catch (IOException e) {
e.printStackTrace();
}
}