二、RandomAccessFile与NIO的读写操作

本文演示了普通的IO操作和NIO操作读取文件,NIO需要注意一下三点:

  • NIO是使用两个指针来读取文件,一个postion一个limit,read和write都会改变position的位置,每次使用完,需要交换position和limit
  • NIO的乱码问题
  • NIO配合channel使用,别忘了关闭Channel,clear Bytebuffer
package com.halfrest.nio.file;


import java.io.*;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

public class RandomAccessFileTest {
    public void write(String path) throws IOException {
        File file = new File(path);
        System.out.println(file.exists());
        FileOutputStream fileOutputStream = new FileOutputStream(file,false);
        fileOutputStream.write("hahaha".getBytes());
        fileOutputStream.flush();
        fileOutputStream.close();
    }


    public void writeNIO(String path) throws IOException {
        File file = new File(path);
        RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
        FileChannel channel = randomAccessFile.getChannel();
        //重置channel到0,如果是第一次启动,不用重置
        channel.position(0);
        ByteBuffer buffer = ByteBuffer.allocate("xhh is yyds".getBytes("UTF-8").length);
        CharBuffer decode = StandardCharsets.UTF_8.decode(buffer);
        //position会到最后
        decode.append(new String("xhh is yyds".getBytes("UTF-8")));
        //交换 position limit
        decode.flip();
        ByteBuffer encode = StandardCharsets.UTF_8.encode(decode);
        channel.write(encode);
        channel.force(true);
        channel.close();
        buffer.clear();
    }

    public String read(String path) throws IOException {
        File file = new File(path);
        RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
        byte[] bytes = new byte[(int) file.length()];
        randomAccessFile.readFully(bytes);
        return new String(bytes);
    }

    public String readNIO(String path) throws IOException {
        File file = new File(path);
        RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
        FileChannel channel = randomAccessFile.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate((int) file.length());
        channel.read(buffer);
        //position走到最后面,需要swap position limit
        buffer.flip();
        //使用UTF-8解码,
        CharBuffer decode = StandardCharsets.UTF_8.decode(buffer);
        channel.close();
        buffer.clear();
        return decode.toString();
    }

    public String readNIODecodeMistake(String path) throws IOException {
        File file = new File(path);
        RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
        FileChannel channel = randomAccessFile.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate((int) file.length());
        channel.read(buffer);
        buffer.flip();
        //使用UTF-8解码
        CharBuffer decode = buffer.asCharBuffer();
        channel.close();
        buffer.clear();
        return decode.toString();
    }

    public static void main(String args[])  {
        RandomAccessFileTest randomAccessFileTest = new RandomAccessFileTest();
        try {
            randomAccessFileTest.write("haha.txt");
            String read = randomAccessFileTest.read("haha.txt");
            System.out.println("read =>"+read);
            randomAccessFileTest.writeNIO("haha.txt");
            System.out.println("readNIO=>"+randomAccessFileTest.readNIO("haha.txt"));
            System.out.println("readNIO readNIODecodeMistake=>"+randomAccessFileTest.readNIODecodeMistake("haha.txt"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值