使用FileChannel和ByteArrays读取ASCII文件
时间:2020-03-06 14:21:49 来源:igfitidea点击:
我有以下代码:
String inputFile = "somefile.txt"; FileInputStream in = new FileInputStream(inputFile); FileChannel ch = in.getChannel(); ByteBuffer buf = ByteBuffer.allocateDirect(BUFSIZE); // BUFSIZE = 256 /* read the file into a buffer, 256 bytes at a time */ int rd; while ( (rd = ch.read( buf )) != -1 ) { buf.rewind(); for ( int i = 0; i < rd/2; i++ ) { /* print each character */ System.out.print(buf.getChar()); } buf.clear(); }
但是字符显示在?的位置。这与使用Unicode字符的Java有关吗?我该如何纠正?
解决方案
是的,它是Unicode。
如果文件中有14个字符,则只会得到7个"?"。
解决方案待定。仍然在想。
是否有特定的原因导致我们以这种方式读取文件?
如果要读取ASCII文件,则应该使用阅读器。
我会做这样的事情:
File inputFile = new File("somefile.txt"); BufferedReader reader = new BufferedReader(new FileReader(inputFile));
然后使用" readLine"或者类似方法实际读取数据!
将打印语句更改为:
System.out.print((char)buf.get());
似乎有帮助。
buf.getChar()预期每个字符2个字节,但我们仅存储1个。使用:
System.out.print((char) buf.get());
根据somefile.txt的编码,字符实际上可能不是由两个字节组成。此页面提供有关如何以正确的编码读取流的更多信息。
令人讨厌的是,文件系统不会告诉我们文件的编码,因为它不知道。就其而言,它只是一堆字节。我们必须找到某种方法将编码传递给程序,以某种方式检测它,或者(如果可能)始终确保编码相同(例如UTF-8)。
我们必须知道文件的编码是什么,然后使用该编码将ByteBuffer解码为CharBuffer。假设文件是ASCII:
import java.util.*; import java.io.*; import java.nio.*; import java.nio.channels.*; import java.nio.charset.*; public class Buffer { public static void main(String args[]) throws Exception { String inputFile = "somefile"; FileInputStream in = new FileInputStream(inputFile); FileChannel ch = in.getChannel(); ByteBuffer buf = ByteBuffer.allocateDirect(BUFSIZE); // BUFSIZE = 256 Charset cs = Charset.forName("ASCII"); // Or whatever encoding you want /* read the file into a buffer, 256 bytes at a time */ int rd; while ( (rd = ch.read( buf )) != -1 ) { buf.rewind(); CharBuffer chbuf = cs.decode(buf); for ( int i = 0; i < chbuf.length(); i++ ) { /* print each character */ System.out.print(chbuf.get()); } buf.clear(); } } }