feat(新增): 🚀 学习文件和文件夹复制Files.walk
This commit is contained in:
parent
1548696620
commit
44708caf0f
|
@ -7,8 +7,45 @@ import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
public class Demo10 {
|
public class Demo10 {
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
|
String source = "D:\\BaiduNetdiskDownload\\web项目\\大屏模板\\【1849】";
|
||||||
|
String target = "D:\\BaiduNetdiskDownload\\web项目\\大屏模板\\【1849】 aaaa";
|
||||||
|
Files.walk(Paths.get(source)).forEach(path -> {
|
||||||
|
try {
|
||||||
|
String targetName = path.toString().replace(source, target);
|
||||||
|
// 是目录 就创建
|
||||||
|
if (Files.isDirectory(path)) {
|
||||||
|
Files.createDirectories(Paths.get(targetName));
|
||||||
|
}
|
||||||
|
// 是普通文件就复制
|
||||||
|
else if (Files.isRegularFile(path)) {
|
||||||
|
Files.copy(path, Paths.get(targetName));
|
||||||
|
}
|
||||||
|
} catch (Exception exception) {
|
||||||
|
exception.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// 删除文件
|
||||||
|
private static void m3() throws IOException {
|
||||||
|
// 删除目录不允许,因为里面有文件,所以不可以删除,先将文件删除之后再删除文件夹
|
||||||
|
Files.walkFileTree(Paths.get("D:\\BaiduNetdiskDownload\\web项目\\大屏模板\\【1849】 - 副本"), new SimpleFileVisitor<>(){
|
||||||
|
// 先删除文件
|
||||||
|
@Override
|
||||||
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||||
|
Files.delete(file);
|
||||||
|
return super.visitFile(file, attrs);
|
||||||
|
}
|
||||||
|
// 再删除文件夹
|
||||||
|
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
||||||
|
Files.delete(dir);
|
||||||
|
return super.postVisitDirectory(dir, exc);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查看当前文件
|
||||||
|
private static void m2() throws IOException {
|
||||||
AtomicInteger atomicInteger = new AtomicInteger();
|
AtomicInteger atomicInteger = new AtomicInteger();
|
||||||
m1();
|
|
||||||
Files.walkFileTree(Paths.get("D:\\BaiduNetdiskDownload\\web项目\\大屏模板"), new SimpleFileVisitor<>(){
|
Files.walkFileTree(Paths.get("D:\\BaiduNetdiskDownload\\web项目\\大屏模板"), new SimpleFileVisitor<>(){
|
||||||
/**
|
/**
|
||||||
* Invoked for a file in a directory.
|
* Invoked for a file in a directory.
|
||||||
|
@ -16,8 +53,6 @@ public class Demo10 {
|
||||||
* <p> Unless overridden, this method returns {@link FileVisitResult#CONTINUE
|
* <p> Unless overridden, this method returns {@link FileVisitResult#CONTINUE
|
||||||
* CONTINUE}.
|
* CONTINUE}.
|
||||||
*
|
*
|
||||||
* @param file
|
|
||||||
* @param attrs
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||||
|
@ -32,6 +67,7 @@ public class Demo10 {
|
||||||
System.out.println("===>" + atomicInteger);
|
System.out.println("===>" + atomicInteger);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 文件相关操作
|
||||||
private static void m1() throws IOException {
|
private static void m1() throws IOException {
|
||||||
AtomicInteger dirCount = new AtomicInteger();
|
AtomicInteger dirCount = new AtomicInteger();
|
||||||
AtomicInteger fileCount = new AtomicInteger();
|
AtomicInteger fileCount = new AtomicInteger();
|
||||||
|
@ -44,8 +80,6 @@ public class Demo10 {
|
||||||
* <p> Unless overridden, this method returns {@link FileVisitResult#CONTINUE
|
* <p> Unless overridden, this method returns {@link FileVisitResult#CONTINUE
|
||||||
* CONTINUE}.
|
* CONTINUE}.
|
||||||
*
|
*
|
||||||
* @param dir
|
|
||||||
* @param attrs
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
||||||
|
@ -60,8 +94,6 @@ public class Demo10 {
|
||||||
* <p> Unless overridden, this method returns {@link FileVisitResult#CONTINUE
|
* <p> Unless overridden, this method returns {@link FileVisitResult#CONTINUE
|
||||||
* CONTINUE}.
|
* CONTINUE}.
|
||||||
*
|
*
|
||||||
* @param file
|
|
||||||
* @param attrs
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue