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和编码的控制。


     

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

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

    文件夹



  • 相关阅读:
    [自定义服务器控件] 第三步:CheckBoxList。
    Flex构建WebService应用
    ServletActionContext.getRequest().getServletPath()错误的解决方法
    MyEclipse 8.6 安装 SVN 插件
    [转]hibernateHQL总结
    Struts入门实例
    错误:“Cannot load JDBC driver class 'com.mysql.jdbc.Driver”的解决方法
    Target runtime Apache Tomcat v6.0 is not defined.错误解决方法
    错误:“已有打开的与此命令相关联的 DataReader,必须首先将它关闭”的解决方法。
    [转]删除Windows 7 C:/Users/用户名/AppData里面的文件
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7354664.html
Copyright © 2011-2022 走看看