java.nio.charset.MalformedInputException: Input length = 1 at java.nio.charset.CoderResult.throwException(CoderResult.java:260) at java.nio.charset.CharsetDecoder.decode(CharsetDecoder.java:781) at cn.fuxi.nio.ReadFile.main(ReadFile.java:37)
public class ReadFile {
	public static void main(String[] args) {
		FileInputStream fis;
		try {
			fis = new FileInputStream("a.txt");
			FileChannel channel = fis.getChannel();
			// 定义一个ByteBuffer,用于重复读取数据
			ByteBuffer byteBuffe = ByteBuffer.allocate(64);// 每次取出64字节
			// 将FileChannel的数据放入ByteBuffer中
			while (channel.read(byteBuffe) != -1) {
				// 锁定ByteBuffer的空白区
				byteBuffe.flip();
				/* 创建Charset对象 */
				Charset charset = Charset.forName("GBK");
				// 创建解码器
				CharsetDecoder charsetDecoder = charset.newDecoder();
				// 将ByteBuffer的内容转码
				CharBuffer charBuffer = charsetDecoder.decode(byteBuffe);
				// CharBuffer charBuffer = charset.decode(byteBuffe);
				System.out.println(charBuffer);
				// 将ByteBuffer初始化,为下一次读取数据做准备
				byteBuffe.clear();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}
This is just test for FileChannel 小心会报异常:java.nio.charset.MalformedInputException: Input length = 1,看到底是什么鬼原因弄成的。
/* 创建Charset对象 */
				Charset charset = Charset.forName("GBK");
				// 创建解码器
				CharsetDecoder charsetDecoder = charset.newDecoder();
				// 将ByteBuffer的内容转码
//				CharBuffer charBuffer = charsetDecoder.decode(byteBuffe);
改为:/* 创建Charset对象 */
				Charset charset = Charset.forName("GBK");
				CharBuffer charBuffer = charset.decode(byteBuffe);This is just test for FileChannel 小心会报异常:java.nio.charset.MalformedInputException: Input length = 1,看到底是什么鬼原因弄成的。 This is just test for FileChannel 小心会报异常:java.nio.charset.MalformedInputException: Input length = 1,看到底? 鞘裁垂碓蚺傻摹?
public class FileChannelTest {
	public static void main(String[] args) {
		try {
			File file=new File("abc.txt");
			//以文件输入流FileInputStream创建FileChannel,以控制输入
			FileChannel inChannel=new FileInputStream(file).getChannel();
			//以文件输出流FileOutputStream创建FileChannel,以控制输出
			FileChannel outChannel=new FileOutputStream("a.txt").getChannel();
			//将FileChannel里的全部数据映射成ByteBuffer
			MappedByteBuffer  buffer=inChannel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
			//直接将buffer里的数据全部输出
			outChannel.write(buffer);
			//再次调用buffer的clear()方法,复原limit、position的位置
			buffer.clear();
			//使用GBK字符集来创建解码器
			Charset charset=Charset.forName("GBK");
			//创建解码器(CharsetDecoder)对象
			CharsetDecoder decoder=charset.newDecoder();
			//使用解码器将ByteBuffer转换成CharBuffer
			CharBuffer charBuffer=decoder.decode(buffer);
			System.out.println(charBuffer);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}==================================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
==================================================================================================
我的Java开发学习之旅------>Java NIO 报java.nio.charset.MalformedInputException: Input length = 1异常
原文:http://blog.csdn.net/ouyang_peng/article/details/46462379