fix:基础练习

上级 0e037396
1234567890abc
\ No newline at end of file
......@@ -35,6 +35,16 @@
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
......
package com.kwan.shuyu.heima.bytebuffer;
package com.kwan.shuyu.heima;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
......
package com.kwan.shuyu.heima.bytebuffer;
package com.kwan.shuyu.heima;
import com.kwan.shuyu.until.ByteBufferUtil;
......
package com.kwan.shuyu.heima.bytebuffer;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* ByteBuffer读取文件内容
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
public class ByteBuffer_001_Test {
public static void main(String[] args) {
try (FileChannel channel = new FileInputStream("data.txt").getChannel()) {
ByteBuffer buffer = ByteBuffer.allocate(10);
channel.read(buffer);
buffer.flip();
while (buffer.hasRemaining()) {
final byte b = buffer.get();
System.out.println((char) b);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.kwan.shuyu.heima.bytebuffer;
import lombok.extern.slf4j.Slf4j;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* ByteBuffer读取文件内容,多次读取
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/4/18 17:15
*/
@Slf4j
public class ByteBuffer_002_Test {
public static void main(String[] args) {
try (FileChannel channel = new FileInputStream("data.txt").getChannel()) {
ByteBuffer buffer = ByteBuffer.allocate(10);
while (true) {
final int len = channel.read(buffer);
log.info("read len {}", len);
if (len == -1) {//表示未读到数据
break;
}
buffer.flip();//读模式
while (buffer.hasRemaining()) {
final byte b = buffer.get();
log.info("实际字节 {}", (char) b);
}
buffer.clear();//写模式
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
helloworld秦书予
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册