fix:文件处理

上级 09577b2b
package com.kwan.shuyu.heima;
/**
* 分割数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public class ByteBuffer_03_split {
public static void main(String[] args) {
}
}
package com.kwan.shuyu.heima;
package com.kwan.shuyu.heima.netty_01_bytebuffer;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
......@@ -12,7 +12,7 @@ import java.nio.charset.StandardCharsets;
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public class ByteBuffer_01_Write {
public class ByteBuffer_12_Test_Write {
public static void main(String[] args) {
final ByteBuffer b1 = StandardCharsets.UTF_8.encode("hello");
final ByteBuffer b2 = StandardCharsets.UTF_8.encode("world");
......
package com.kwan.shuyu.heima.netty_01_bytebuffer;
import java.nio.ByteBuffer;
import static com.kwan.shuyu.until.ByteBufferUtil.debugAll;
/**
* 分割数据
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public class ByteBuffer_13_Test_split {
public static void main(String[] args) {
final ByteBuffer source = ByteBuffer.allocate(32);
source.put("Hello, world\nI'm zhangsan \nho".getBytes());
split(source);
source.put("w are you?\n".getBytes());
split(source);
}
private static void split(ByteBuffer source) {
source.flip();
final int limit = source.limit();
for (int i = 0; i < limit; i++) {
//找到完整的消息
if (source.get(i) == '\n') {
final int len = i + 1 - source.position();
final ByteBuffer target = ByteBuffer.allocate(len);
for (int j = 0; j < len; j++) {
//从source读取,向target写入
target.put(source.get());
}
debugAll(target);
}
}
source.compact();
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_02_file;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* 创建目录
* 如果目录已存在,会抛异常FileAlreadyExistsException
* 不能一次创建多级目录,否则会抛异常NoSuchFileException
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public class File_02_Test_create {
public static void main(String[] args) throws IOException {
final Path path = Paths.get("/Users/qinyingjie/Documents/idea-workspace/netty-demo/src/main/java/com/kwan/shuyu/heima/1111");
Files.createDirectories(path);
}
}
package com.kwan.shuyu.heima.netty_02_file;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* 创建多级目录
* 如果目录已存在,会抛异常FileAlreadyExistsException
* 不能一次创建多级目录,否则会抛异常NoSuchFileException
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public class File_03_Test_create_more {
public static void main(String[] args) throws IOException {
final Path path = Paths.get("/Users/qinyingjie/Documents/idea-workspace/netty-demo/src/main/java/com/kwan/shuyu/heima/222/333");
Files.createDirectories(path);
}
}
package com.kwan.shuyu.heima.netty_02_file;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
/**
* 将一个文件中的数据复制到另一个文件,只传一次
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public class File_04_Test_transferTo {
public static void main(String[] args) {
long start = System.nanoTime();
try (final FileChannel from = new FileInputStream("data.txt").getChannel();
final FileChannel to = new FileOutputStream("to.txt").getChannel();
) {
final long size = from.size();
//效率高,底层会利用操作系统的季零拷贝进行优化
from.transferTo(0, size, to);
} catch (IOException e) {
e.printStackTrace();
}
long end = System.nanoTime();
System.out.println("transferTo 用时:" + (end - start) / 1000_000.0);
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_02_file;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
/**
* 将一个文件中的数据复制到另一个文件 传多次
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public class File_05_Test_transferTo {
public static void main(String[] args) {
try (final FileChannel from = new FileInputStream("data.txt").getChannel();
final FileChannel to = new FileOutputStream("to.txt").getChannel();
) {
final long size = from.size();
//left代表还剩多少字节
for (long left = size; left > 0; ) {
left -= from.transferTo((size - left), left, to);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_02_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;
/**
* 将一个文件中的数据复制到另一个文件 传多次
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public class File_06_Test_copy {
public static void main(String[] args) {
try {
Path source = Paths.get("data.txt");
Path target = Paths.get("to.txt");
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_02_file;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public class File_07_Test_walkFileTree {
public static void main(String[] args) throws IOException {
final AtomicInteger dirCount = new AtomicInteger();
final AtomicInteger fileCount = new AtomicInteger();
Files.walkFileTree(Paths.get("/Users/qinyingjie/Documents/idea-workspace/netty-demo"), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
System.out.println("==========>>>" + dir);
dirCount.incrementAndGet();
return super.preVisitDirectory(dir, attrs);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("==========>>>" + file);
fileCount.incrementAndGet();
return super.visitFile(file, attrs);
}
});
System.out.println("文件夹数量=" + dirCount);
System.out.println("文件数量=" + fileCount);
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_02_file;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public class File_08_Test_jar {
public static void main(String[] args) throws IOException {
final AtomicInteger jarCount = new AtomicInteger();
Files.walkFileTree(Paths.get("/Users/qinyingjie/Documents/idea-workspace/netty-demo"), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.toString().endsWith(".jar")) {
System.out.println("==========>>>" + file);
jarCount.incrementAndGet();
}
return super.visitFile(file, attrs);
}
});
System.out.println("jar包数量=" + jarCount);
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_02_file;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
/**
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public class File_09_Test_delete {
public static void main(String[] args) throws IOException {
Files.walkFileTree(Paths.get("/Users/qinyingjie/Downloads/ConcurrntHashMap扩容源码分析_副本"), 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);
}
});
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_02_file;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* 拷贝一个文件夹中的数据到另一个文件夹(有子文件夹)
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public class File_10_Test_copy {
public static void main(String[] args) throws IOException {
String from = "/Users/qinyingjie/Documents/idea-workspace/netty-demo";
String to = "/Users/qinyingjie/Downloads/未命名文件夹";
Files.walk(Paths.get(from)).forEach(path -> {
try {
String targetName = path.toString().replace(from, to);
if (Files.isDirectory(path)) {
Files.createDirectories(Paths.get(targetName));
}
if (Files.isRegularFile(path)) {
Files.copy(path, Paths.get(targetName));
}
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
\ No newline at end of file
package com.kwan.shuyu.heima.netty_03_nio.nio_03_selector;
import com.kwan.shuyu.until.ByteBufferUtil;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
/**
* NioServer 服务端 NioServerAttachment 附件传递
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 18:22
*/
@Slf4j
public class NioServerAttachment {
public static void main(String[] args) throws IOException {
final Selector selector = Selector.open();
//预设ByteBuffer,并分配空间,为了读取数据
final ByteBuffer buffer = ByteBuffer.allocate(16);
//创建ServerSocketChannel
final ServerSocketChannel ssc = ServerSocketChannel.open();
//绑定端口号
ssc.configureBlocking(false);
//ServerSocketChannel注册到Selector中
final SelectionKey sscKey = ssc.register(selector, 0, null);
sscKey.interestOps(SelectionKey.OP_ACCEPT);
log.info("sscKey={}", sscKey);
ssc.bind(new InetSocketAddress(8080));
while (true) {
//select方法,没有事件发生,线程阻塞,有事件,线程才会恢复运行
//select在事件未处理时,它不会阻塞,会一直请求处理
selector.select();
final Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while (iterator.hasNext()) {
final SelectionKey key = iterator.next();
iterator.remove();
log.info("key={}", key);
if (key.isAcceptable()) {
final ServerSocketChannel channel = (ServerSocketChannel) key.channel();
final SocketChannel sc = channel.accept();
sc.configureBlocking(false);
ByteBuffer bf = ByteBuffer.allocate(16);
//将一个ByteBuffer作为附件绑定到SelectionKey
final SelectionKey scKey = sc.register(selector, 0, bf);
scKey.interestOps(SelectionKey.OP_READ);
log.info("sc={}", sc);
log.info("scKey={}", scKey);
} else if (key.isReadable()) {
try {
final SocketChannel channel = (SocketChannel) key.channel();
//获取附件的ByteBuffer
final ByteBuffer bf = (ByteBuffer) key.attachment();
final int read = channel.read(bf);
if (read == -1){
}else{
}
bf.flip();
ByteBufferUtil.debugRead(bf);
} catch (IOException e) {
e.printStackTrace();
key.cancel();//取消事件处理
}
}
}
}
}
}
\ No newline at end of file
1234567890abc
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册