0%

java | 遍历目录中的文件

遍历目录中的文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.redisc;

import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

@Slf4j(topic = "c.Test")
public class Run {

public static void main(String[] args) throws IOException {
Files.walkFileTree(Paths.get("src/main"), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("====>" + dir);
return super.preVisitDirectory(dir, attrs);
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println(file);
return super.visitFile(file, attrs);
}
});
}

}

删除文件夹和文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.redisc;

import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

@Slf4j(topic = "c.Test")
public class Run {

public static void main(String[] args) throws IOException {
m1();
}

private static void m1() throws IOException {
Files.walkFileTree(Paths.get("src/main"), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("进入====>" + dir);
Files.delete(dir);
return super.preVisitDirectory(dir, attrs);
}

@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 {
System.out.println("退出====>" + dir);
Files.delete(dir);
return super.postVisitDirectory(dir, exc);
}
});
}

}

拷贝目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.redisc;

import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

@Slf4j(topic = "c.Test")
public class Run {

public static void main(String[] args) throws IOException {
m1();
}

private static void m1() throws IOException {
String source = "";
String target = "";
Files.walk(Paths.get(source).forEach(path -> {
String targetName = path.toString().replace(source, target);
// 是目录
if (Files.isDirectory(path)) {
try {
Files.createDirectory(Paths.get(targetName));
} catch (IOException e) {
e.printStackTrace();
}
} else if (Files.isRegularFile(path)) {
try {
Files.copy(path, Paths.get(targetName));
} catch (IOException e) {
e.printStackTrace();
}
}
})
}

}
请我喝杯咖啡吧~