package io;import io.txtfile.TxtFile;import java.io.IOException;import java.io.RandomAccessFile;import java.nio.ByteBuffer;import java.nio.CharBuffer;import java.nio.channels.FileChannel;import java.nio.charset.Charset;import java.nio.charset.CharsetDecoder;public class ReadFile {static void readFile(String fileName, String charset) throws IOException {RandomAccessFile rf = new RandomAccessFile(fileName, "r");FileChannel rfc = rf.getChannel();ByteBuffer byteBuf = ByteBuffer.allocate(100);CharsetDecoder decoder = Charset.forName(charset).newDecoder();CharBuffer charBuf = CharBuffer.allocate(100);char[] cs = new char[charBuf.limit()];int len = 0;while ((len = rfc.read(byteBuf)) != -1) {byteBuf.flip();decoder.decode(byteBuf, charBuf, false);byteBuf.compact();charBuf.flip();System.out.print(charBuf.toString());charBuf.clear();}}public static void main(String[] args) throws IOException {readFile(TxtFile.UTF8, "UTF-8");// readFile("e:\\gbk.txt", "GBK");}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。