每当遇到一些莫名其妙的错误,怎么都排查不出来的时候,那个错误一定是个非常无脑的问题。
问题描述
在看 NIO 时,自己尝试了下它的 API;于是在资源目录下创建了 a.txt
、b.tzt
、c.txt
,but,在我尝试读取时并没有出现问题,但是在写入时一直有问题(写入后通过 idea 查看资源目录下的 txt 文件发现并没有写入(还是空文件)),并且得不到解决;以下是 “bug” 代码:
public class Test {
public static void main(String[] args) throws IOException {
// 准备缓冲区
ByteBuffer buffer = ByteBuffer.allocate(128);
// 读取文件
try (
FileInputStream fis = new FileInputStream(getPath("io/a.txt"));
FileChannel channel = fis.getChannel();
) {