feat(新增): 🚀 学习文件和文件夹操作,遍历,查找,复制,删除

This commit is contained in:
Bunny 2024-05-22 22:39:49 +08:00
parent 5df9a0d8d0
commit 1548696620
79 changed files with 7981 additions and 7 deletions

20
.idea/jarRepositories.xml Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

View File

@ -8,7 +8,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

1
data.txt Normal file
View File

@ -0,0 +1 @@
helloworld你好

View File

@ -0,0 +1,77 @@
package cn.bunny;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;
public class Demo10 {
public static void main(String[] args) throws IOException {
AtomicInteger atomicInteger = new AtomicInteger();
m1();
Files.walkFileTree(Paths.get("D:\\BaiduNetdiskDownload\\web项目\\大屏模板"), new SimpleFileVisitor<>(){
/**
* Invoked for a file in a directory.
*
* <p> Unless overridden, this method returns {@link FileVisitResult#CONTINUE
* CONTINUE}.
*
* @param file
* @param attrs
*/
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.endsWith(".jpg")) {
System.out.println(file);
atomicInteger.incrementAndGet();
}
return super.visitFile(file, attrs);
}
});
System.out.println("===>" + atomicInteger);
}
private static void m1() throws IOException {
AtomicInteger dirCount = new AtomicInteger();
AtomicInteger fileCount = new AtomicInteger();
// 访问者模式
Files.walkFileTree(Paths.get("D:\\BaiduNetdiskDownload\\web项目\\大屏模板"), new SimpleFileVisitor<>() {
/**
* Invoked for a directory before entries in the directory are visited.
*
* <p> Unless overridden, this method returns {@link FileVisitResult#CONTINUE
* CONTINUE}.
*
* @param dir
* @param attrs
*/
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("===>"+dir);
dirCount.incrementAndGet();
return super.preVisitDirectory(dir, attrs);
}
/**
* Invoked for a file in a directory.
*
* <p> Unless overridden, this method returns {@link FileVisitResult#CONTINUE
* CONTINUE}.
*
* @param file
* @param attrs
*/
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("===>"+file);
fileCount.decrementAndGet();
return super.visitFile(file, attrs);
}
});
System.out.println("文件夹的===>" + dirCount);
System.out.println("文件的===>" + fileCount);
}
}

View File

@ -1,6 +1,5 @@
package cn.bunny;
import java.nio.ByteBuffer;
public class Demo3 {
@ -10,6 +9,7 @@ public class Demo3 {
buffer.put((byte) 0x61);
buffer.put(new byte[]{0x62, 0x63, 0x64});
// 共有4个值
System.out.println(buffer);
}
}

View File

@ -3,6 +3,7 @@ package cn.bunny;
import java.nio.ByteBuffer;
public class Demo4 {
public static void main(String[] args) {
ByteBuffer buffer = ByteBuffer.allocate(10);
buffer.put(new byte[]{'a', 'b', 'c', 'd', 'e'});
@ -16,5 +17,8 @@ public class Demo4 {
// mark & reset
System.out.println((char) buffer.get());// b
System.out.println((char) buffer.get());// c
// get(i) 不会改变读索引
System.out.println((char) buffer.get());
}
}

View File

@ -0,0 +1,26 @@
package cn.bunny;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
public class Demo5 {
public static void main(String[] args) {
// 将字符串转成ByteBuffer
ByteBuffer buffer = ByteBuffer.allocate(16);
buffer.put("hello".getBytes());
// 转换方式二
ByteBuffer buffer1 = StandardCharsets.UTF_8.encode("hello");
System.out.println(buffer1);
// 转换方式三
ByteBuffer buffer2 = ByteBuffer.wrap("hello".getBytes());
System.out.println(buffer2);
// 使用读模式才能有值
buffer1.flip();
String str1 = StandardCharsets.UTF_8.decode(buffer1).toString();
System.out.println(str1);
}
}

View File

@ -0,0 +1,23 @@
package cn.bunny;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
public class Demo6 {
public static void main(String[] args) {
ByteBuffer b1Buffer = StandardCharsets.UTF_8.encode("hello");
ByteBuffer b2Buffer = StandardCharsets.UTF_8.encode("world");
ByteBuffer b3Buffer = StandardCharsets.UTF_8.encode("你好");
try (FileChannel channel = new RandomAccessFile("data.txt", "rw").getChannel()) {
channel.write(new ByteBuffer[]{b1Buffer, b2Buffer, b3Buffer});
} catch (IOException exception) {
}
}
}

View File

@ -0,0 +1,31 @@
package cn.bunny;
import java.nio.ByteBuffer;
public class Demo7 {
public static void main(String[] args) {
// 黏包 半包
// 黏包很多字符串连在一起
// 半包有部分没有发送完成
ByteBuffer buffer = ByteBuffer.allocate(32);
buffer.put("Hello World!\nI'm running\nHo".getBytes());
split(buffer);
buffer.put("w are you!".getBytes());
// split(buffer);
}
private static void split(ByteBuffer buffer) {
buffer.flip();
for (int i = 0; i < buffer.limit(); i++) {
if (buffer.get(i) == '\n') {
int len = i + 1 - buffer.position();
ByteBuffer target = ByteBuffer.allocate(len);
for (int j = 0; j < len; j++) {
// byte b = buffer.get();
target.put(buffer.get());
}
}
}
}
}

View File

@ -0,0 +1,25 @@
package cn.bunny;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
public class Demo8 {
public static void main(String[] args) {
// 只能传输 2g
try (
FileChannel fileChannel = new FileInputStream("data.txt").getChannel();
FileChannel channel = new FileOutputStream("to.txt").getChannel();
) {
// 针对只能传输两个G做了以下优化
long size = fileChannel.size();
// 分多次传输两个G内容
for (long left = size; left > 0; ) {
left -= fileChannel.transferTo((size - left), left, channel);
}
// fileChannel.transferTo(0, fileChannel.size(), channel);
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,37 @@
package cn.bunny;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class Demo9 {
public static void main(String[] args) throws IOException {
Path path = Paths.get("D:\\BaiduNetdiskDownload\\java项目\\目录");
// 正常化路径
System.out.println(path.normalize());
// 判断文件是否存在
System.out.println(Files.exists(path));
// 创建单层目录
Files.createDirectory(path);
// 创建多层目录
Files.createDirectories(path);
// 拷贝文件
Path source = Paths.get("data.txt");
Path target = Paths.get("target.txt");
Files.copy(source, target);
// 如果存在文件覆盖文件
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
// 拷贝保证文件原子性
Files.copy(source, target, StandardCopyOption.ATOMIC_MOVE);
// 删除文件只能删除空目录如果有内容无法删除
Files.delete(path);
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,3 @@
cn\bunny\Demo5.class
cn\bunny\Demo7.class
cn\bunny\Demo6.class

View File

@ -1,5 +1,7 @@
D:\MyFolder\Netty\bytebuffter\demo1\src\main\java\cn\bunny\ByteBufferUtil.java
D:\MyFolder\Netty\bytebuffter\demo1\src\main\java\cn\bunny\Demo01.java
D:\MyFolder\Netty\bytebuffter\demo1\src\main\java\cn\bunny\Demo3.java
D:\MyFolder\Netty\bytebuffter\demo1\src\main\java\cn\bunny\Demo02.java
D:\MyFolder\Netty\bytebuffter\demo1\src\main\java\cn\bunny\Main.java
D:\BaiduNetdiskDownload\java项目\netty\demo1\src\main\java\cn\bunny\Demo3.java
D:\BaiduNetdiskDownload\java项目\netty\demo1\src\main\java\cn\bunny\Demo01.java
D:\BaiduNetdiskDownload\java项目\netty\demo1\src\main\java\cn\bunny\Demo02.java
D:\BaiduNetdiskDownload\java项目\netty\demo1\src\main\java\cn\bunny\Demo4.java
D:\BaiduNetdiskDownload\java项目\netty\demo1\src\main\java\cn\bunny\Demo7.java
D:\BaiduNetdiskDownload\java项目\netty\demo1\src\main\java\cn\bunny\Demo5.java
D:\BaiduNetdiskDownload\java项目\netty\demo1\src\main\java\cn\bunny\Demo6.java

BIN
nio/ByteBuffer_01.class Normal file

Binary file not shown.

2282
nio/文档/Netty01-nio.md Normal file

File diff suppressed because it is too large Load Diff

1871
nio/文档/Netty02-入门.md Normal file

File diff suppressed because it is too large Load Diff

1670
nio/文档/Netty03-进阶.md Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

BIN
nio/文档/img/0001.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 72 KiB

BIN
nio/文档/img/0002.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

BIN
nio/文档/img/0003.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

BIN
nio/文档/img/0004.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
nio/文档/img/0005.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 122 KiB

BIN
nio/文档/img/0006.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

BIN
nio/文档/img/0007.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
nio/文档/img/0008.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

BIN
nio/文档/img/0009.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

BIN
nio/文档/img/0010.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
nio/文档/img/0011.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
nio/文档/img/0012.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

BIN
nio/文档/img/0013.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

BIN
nio/文档/img/0014.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

BIN
nio/文档/img/0015.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 92 KiB

BIN
nio/文档/img/0016.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 98 KiB

BIN
nio/文档/img/0017.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

BIN
nio/文档/img/0018.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

BIN
nio/文档/img/0019.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.1 KiB

BIN
nio/文档/img/0020.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB

BIN
nio/文档/img/0021.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

BIN
nio/文档/img/0022.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

BIN
nio/文档/img/0023.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
nio/文档/img/0024.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.8 KiB

BIN
nio/文档/img/0025.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
nio/文档/img/0026.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.6 KiB

BIN
nio/文档/img/0027.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

BIN
nio/文档/img/0028.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 232 KiB

BIN
nio/文档/img/0029.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 276 KiB

BIN
nio/文档/img/0030.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 324 KiB

BIN
nio/文档/img/0031.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 187 KiB

BIN
nio/文档/img/0032.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 KiB

BIN
nio/文档/img/0033.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
nio/文档/img/0034.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

BIN
nio/文档/img/0035.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
nio/文档/img/0036.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

BIN
nio/文档/img/0037.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

BIN
nio/文档/img/0038.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

BIN
nio/文档/img/0039.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
nio/文档/img/0040.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 238 KiB

BIN
nio/文档/img/0041.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

BIN
nio/文档/img/0042.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

BIN
nio/文档/img/0043.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

BIN
nio/文档/img/0044.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

BIN
nio/文档/img/0046.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

BIN
nio/文档/img/0047.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

BIN
nio/文档/img/0048.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

BIN
nio/文档/img/0049.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
nio/文档/img/0050.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

BIN
nio/文档/img/0051.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

1
to.txt Normal file
View File

@ -0,0 +1 @@
helloworld你好