用FileChannel和ByteArrays读取ASCII文件 [英] Reading an ASCII file with FileChannel and ByteArrays

查看:125
本文介绍了用FileChannel和ByteArrays读取ASCII文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

  String inputFile =somefile.txt; 
FileInputStream in = new FileInputStream(inputFile);
FileChannel ch = in.getChannel();
ByteBuffer buf = ByteBuffer.allocateDirect(BUFSIZE); // BUFSIZE = 256

/ *将文件读入一个缓冲区,一次256字节* /
int rd; ((rd = ch.read(buf))!= -1){
buf.rewind();
for(int i = 0; i< rd / 2; i ++){
/ *打印每个字符* /
System.out.print(buf.getChar());
}
buf.clear();
}

但是角色会显示在?这是否与使用Unicode字符的Java有关?如何解决这个问题?解决方案

你必须知道文件的编码是什么,然后将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); //或者你想要的任何编码

/ *将文件读入一个缓冲区,一次256字节* /
int rd; ((rd = ch.read(buf))!= -1){
buf.rewind();
CharBuffer chbuf = cs.decode(buf);
for(int i = 0; i< chbuf.length(); i ++){
/ *打印每个字符* /
System.out.print(chbuf.get()) ;
}
buf.clear();
}
}
}


I have the following code:

        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();
        }

But the characters get displayed at ?'s. Does this have something to do with Java using Unicode characters? How do I correct this?

解决方案

You have to know what the encoding of the file is, and then decode the ByteBuffer into a CharBuffer using that encoding. Assuming the file is 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();
        }
    }
}

这篇关于用FileChannel和ByteArrays读取ASCII文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆