feat(新增): 🚀 学习文件和文件夹操作,遍历,查找,复制,删除
|
@ -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>
|
|
@ -8,7 +8,7 @@
|
||||||
</list>
|
</list>
|
||||||
</option>
|
</option>
|
||||||
</component>
|
</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" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,5 @@
|
||||||
package cn.bunny;
|
package cn.bunny;
|
||||||
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
public class Demo3 {
|
public class Demo3 {
|
||||||
|
@ -10,6 +9,7 @@ public class Demo3 {
|
||||||
buffer.put((byte) 0x61);
|
buffer.put((byte) 0x61);
|
||||||
buffer.put(new byte[]{0x62, 0x63, 0x64});
|
buffer.put(new byte[]{0x62, 0x63, 0x64});
|
||||||
|
|
||||||
|
// 共有4个值
|
||||||
System.out.println(buffer);
|
System.out.println(buffer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package cn.bunny;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
public class Demo4 {
|
public class Demo4 {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ByteBuffer buffer = ByteBuffer.allocate(10);
|
ByteBuffer buffer = ByteBuffer.allocate(10);
|
||||||
buffer.put(new byte[]{'a', 'b', 'c', 'd', 'e'});
|
buffer.put(new byte[]{'a', 'b', 'c', 'd', 'e'});
|
||||||
|
@ -16,5 +17,8 @@ public class Demo4 {
|
||||||
// mark & reset
|
// mark & reset
|
||||||
System.out.println((char) buffer.get());// b
|
System.out.println((char) buffer.get());// b
|
||||||
System.out.println((char) buffer.get());// c
|
System.out.println((char) buffer.get());// c
|
||||||
|
|
||||||
|
// get(i) 不会改变读索引
|
||||||
|
System.out.println((char) buffer.get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
cn\bunny\Demo5.class
|
||||||
|
cn\bunny\Demo7.class
|
||||||
|
cn\bunny\Demo6.class
|
|
@ -1,5 +1,7 @@
|
||||||
D:\MyFolder\Netty\bytebuffter\demo1\src\main\java\cn\bunny\ByteBufferUtil.java
|
D:\BaiduNetdiskDownload\java项目\netty\demo1\src\main\java\cn\bunny\Demo3.java
|
||||||
D:\MyFolder\Netty\bytebuffter\demo1\src\main\java\cn\bunny\Demo01.java
|
D:\BaiduNetdiskDownload\java项目\netty\demo1\src\main\java\cn\bunny\Demo01.java
|
||||||
D:\MyFolder\Netty\bytebuffter\demo1\src\main\java\cn\bunny\Demo3.java
|
D:\BaiduNetdiskDownload\java项目\netty\demo1\src\main\java\cn\bunny\Demo02.java
|
||||||
D:\MyFolder\Netty\bytebuffter\demo1\src\main\java\cn\bunny\Demo02.java
|
D:\BaiduNetdiskDownload\java项目\netty\demo1\src\main\java\cn\bunny\Demo4.java
|
||||||
D:\MyFolder\Netty\bytebuffter\demo1\src\main\java\cn\bunny\Main.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
|
||||||
|
|
After Width: | Height: | Size: 72 KiB |
After Width: | Height: | Size: 32 KiB |
After Width: | Height: | Size: 26 KiB |
After Width: | Height: | Size: 31 KiB |
After Width: | Height: | Size: 122 KiB |
After Width: | Height: | Size: 20 KiB |
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 15 KiB |
After Width: | Height: | Size: 30 KiB |
After Width: | Height: | Size: 17 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 9.4 KiB |
After Width: | Height: | Size: 50 KiB |
After Width: | Height: | Size: 102 KiB |
After Width: | Height: | Size: 92 KiB |
After Width: | Height: | Size: 98 KiB |
After Width: | Height: | Size: 104 KiB |
After Width: | Height: | Size: 8.5 KiB |
After Width: | Height: | Size: 8.1 KiB |
After Width: | Height: | Size: 8.9 KiB |
After Width: | Height: | Size: 7.5 KiB |
After Width: | Height: | Size: 26 KiB |
After Width: | Height: | Size: 11 KiB |
After Width: | Height: | Size: 8.8 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 7.6 KiB |
After Width: | Height: | Size: 7.9 KiB |
After Width: | Height: | Size: 232 KiB |
After Width: | Height: | Size: 276 KiB |
After Width: | Height: | Size: 324 KiB |
After Width: | Height: | Size: 187 KiB |
After Width: | Height: | Size: 174 KiB |
After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 31 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 18 KiB |
After Width: | Height: | Size: 16 KiB |
After Width: | Height: | Size: 14 KiB |
After Width: | Height: | Size: 238 KiB |
After Width: | Height: | Size: 59 KiB |
After Width: | Height: | Size: 36 KiB |
After Width: | Height: | Size: 29 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 3.6 KiB |
After Width: | Height: | Size: 33 KiB |
After Width: | Height: | Size: 4.6 KiB |
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 38 KiB |
After Width: | Height: | Size: 34 KiB |