码迷,mamicode.com
首页 > 其他好文 > 详细

NIO 中文乱码自我解决的简单DEMO

时间:2018-05-04 18:33:04      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:ann   utils   nio   port   file   str   技术   hand   orm   

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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;

import org.apache.commons.lang.time.DurationFormatUtils;


public class NIOTest {
    
    public static final int SIZE = 102400;
    public static final String PATH = "D:\\test\\test.txt";
    
    public static void main(String args[]) {
        
        //方法一测试通过
        /*
         try {//写数据
            FileChannel fileOutChannel = new FileOutputStream(PATH,true).getChannel();
            fileOutChannel.write(ByteBuffer.wrap("这是用FileOutpuStream调用NIO的Channel写出的内容".getBytes()));
            System.out.println("写出成功!");
            fileOutChannel.close();
            //读数据
            FileChannel fileInChannel = new FileInputStream(PATH).getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate(SIZE);
            fileInChannel.read(byteBuffer);
            byteBuffer.flip();
            Charset charset = Charset.forName("UTF-8");
            while (byteBuffer.hasRemaining()) {
                CharsetDecoder charsetDecoder = charset.newDecoder();
                CharBuffer charBuffer = charsetDecoder.decode(byteBuffer);
                System.out.print(charBuffer);                   
            }
            System.out.println();
            System.out.println("读入完成!");
            fileInChannel.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        */
        
        //方法二未经测试
        try {
            FileChannel fileChannel = new RandomAccessFile(PATH, "rw").getChannel();
            //写数据
            fileChannel.write(ByteBuffer.wrap("这是用RandomAccessFile调用NIO的Channel写出的内容".getBytes()));
            System.out.println("写出成功!");
            //读数据
            ByteBuffer byteBuffer = ByteBuffer.allocate(SIZE);
            fileChannel.read(byteBuffer);
            byteBuffer.flip();
            Charset charset = Charset.forName("UTF-8");
            while (byteBuffer.hasRemaining()) {
                CharsetDecoder charsetDecoder = charset.newDecoder();
                CharBuffer charBuffer = charsetDecoder.decode(byteBuffer);
                System.out.print(charBuffer);               
            }
            System.out.println();
            System.out.println("读入成功!");
            fileChannel.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO: handle exception
        }
        
    }
    
    
    

}

查看方法一运行效果:
技术分享图片

NIO 中文乱码自我解决的简单DEMO

标签:ann   utils   nio   port   file   str   技术   hand   orm   

原文地址:https://www.cnblogs.com/jpfss/p/8991551.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!