zoukankan      html  css  js  c++  java
  • 从头认识java-16.5 nio的数据转换

    这一章节我们来讨论一些nio的数据转换。

    上面一章节我们提到ByteBuffer,可是他是面向二进制数据,对于编程来说不是非常方便,因此,java添加了转换数据的工具。

    1.asCharBuffer

    package com.ray.ch16;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    public class Test {
    
    	public static void main(String[] args) throws IOException {
    		String path = "d://123.txt";
    		FileOutputStream fileOutputStream = new FileOutputStream(path);
    		FileChannel fileChannel = fileOutputStream.getChannel();
    		ByteBuffer buffer = ByteBuffer.allocate(24);
    		buffer.asCharBuffer().put("welcome");//在这里能够方便的使用String就能够把元素放到缓冲区
    		while (buffer.hasRemaining()) {
    			fileChannel.write(buffer);
    		}
    		fileChannel.close();
    
    		FileInputStream fileInputStream = new FileInputStream(path);
    		fileChannel = fileInputStream.getChannel();
    		fileChannel.read(buffer);
    		buffer.flip();
    		System.out.println(buffer.asCharBuffer());
    		fileChannel.close();
    	}
    }
    

    输出:

    welcome(事实上后面另一些乱码,这里显示不出来)

    注意:这里的乱码事实上是剩余的字符,welcome使用了14个字符,剩余的10个字符也会产生,然后打印出来。 我们在详细的文件中面就能够看见,welcome后面还跟着一堆空格。

    2.控制输出输入的编码

    package com.ray.ch14;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    
    public class Test {
    	public static void main(String[] args) throws IOException {
    		String path = "d://123.txt";
    		FileOutputStream fileOutputStream = new FileOutputStream(path);
    		FileChannel fileChannel = fileOutputStream.getChannel();
    		ByteBuffer buffer = ByteBuffer.wrap("hello world".getBytes("UTF-8"));//控制输入的编码
    		fileChannel.write(buffer);
    		fileChannel.close();
    		fileOutputStream.close();
    
    		System.out.println(System.getProperty("file.encoding"));
    
    		FileInputStream fileInputStream = new FileInputStream(path);
    		fileChannel = fileInputStream.getChannel();
    		fileChannel.read(buffer);
    		buffer.flip();
    		System.out.println(buffer.asCharBuffer());
    		fileChannel.close();
    		fileInputStream.close();
    
    	}
    }
    

    输出:

    GBK
    桥汬漠睯牬

    因为我当前的编码是GBK。因此输出的时候发生乱码。

    以下是我改动过的代码:

    package com.ray.ch14;
    
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.channels.FileChannel;
    import java.nio.charset.Charset;
    
    public class Test {
    	public static void main(String[] args) throws IOException {
    		String path = "d://123.txt";
    		FileOutputStream fileOutputStream = new FileOutputStream(path);
    		FileChannel fileChannel = fileOutputStream.getChannel();
    		ByteBuffer buffer = ByteBuffer.wrap("hello world".getBytes("UTF-8"));
    		fileChannel.write(buffer);
    		fileChannel.close();
    		fileOutputStream.close();
    
    		System.out.println(System.getProperty("file.encoding"));
    
    		FileInputStream fileInputStream = new FileInputStream(path);
    		fileChannel = fileInputStream.getChannel();
    		fileChannel.read(buffer);
    		buffer.flip();
    		String encoding = "UTF-8";
    		System.out.println(Charset.forName(encoding).decode(buffer));//这里使用跟上面同样的编码解码
    		fileChannel.close();
    		fileInputStream.close();
    
    	}
    }
    

    输出:

    GBK
    hello world

    哪怕我的默认编码是GBK。可是也能够解码出原来的字符串。

    总结:这一章节介绍nio的asCharBuffer和编码的控制。


     

    这一章节就到这里。谢谢。

    -----------------------------------

    文件夹



  • 相关阅读:
    AODV路由协议的路由缓存队列详解
    NS各种常用资料(转)
    Zigbee之旅(二):第一个CC2430程序——LED灯闪烁实验(转)
    计算机核心期刊一览【转】
    NS2中能量模型的添加
    Zigbee之旅(一):开天辟地(转)
    NS2能量模型
    Zigbee之旅(三):几个重要的CC2430基础实验——外部中断(转)
    如何画MDI主窗体的背景
    Speed up the display of Delphi list components
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7354664.html
Copyright © 2011-2022 走看看