NIO 中文乱码自我解决的简单DEMO
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 {
RandomAccessFile randomAccessFile = new RandomAccessFile(PATH, "rw");
//写数据
randomAccessFile.writeUTF("这是用RandomAccessFile调用NIO的Channel写出的内容");
System.out.println("写出成功!");
//读数据
randomAccessFile.seek(0);
System.out.println(randomAccessFile.readUTF());
System.out.println("读入成功!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO: handle exception
}
}
}
查看方法一运行效果:

NIO 中文乱码自我解决的简单DEMO的更多相关文章
- NIO 中文乱码问题的解决代码实现
之前在网上查询了很多关于解决NIO中文乱码的问题,仁者见仁智者见智,不过就找到的几种方法实现都太繁琐了,稍微研究了下NIO源码,以下是我自己的一种实现,偷懒用最简单的代码去实现是我的习惯! Demo: ...
- Win 7英文系统显示中文乱码的解决(转)
Win 7英文系统显示中文乱码的解决http://www.enet.com.cn/article/2011/0811/A20110811896633.shtml 请点击Startmenu并点击Cont ...
- php 使用phpmailer 发送邮件(附带中文乱码的解决方法)
下载phpmailer ,在程序里包含class.phpmailer.php 类 ,这里有中文乱码的解决方法 实例代码如下 <html> <head> <title&g ...
- C#中WebClient使用DownloadString中文乱码的解决办法
原文:C#中WebClient中文乱码的解决办法 第一次尝试: string question = textBox1.Text.ToString(); WebClient client= new We ...
- JSP的学习(4)——中文乱码的解决
本篇将以JSP页面中可能存在的中文乱码问题进行分析和解决. 中文乱码的问题一直是国人在编程过程中的一大头疼问题,这点上在JSP.Servlet或Tomcat上随处可见.比如我们在写一个Servlet时 ...
- 详解get请求和post请求参数中文乱码的解决办法
首先出现中文乱码的原因是tomcat默认的编码方式是"ISO-8859-1",这种编码方式以单个字节作为一个字符,而汉字是以两个字节表示一个字符的. 一,get请求参数中文乱码的解 ...
- xShell终端中文乱码完全解决方法
xShell终端中文乱码完全解决方法 xShell(xShell5)以及其他终端中文乱码的原因无非有三种:(1)Linux系统的编码问题:(2)xShell终端的编码问题: (3)两端的语言编码不一致 ...
- Source Insight 4 中文乱码的解决办法(source insight 3.5 及以下版本就到其他地方看看吧)
干货:Source Insight 4 中文乱码的解决办法(source insight 3.5 及以下版本就到其他地方看看吧) [解决办法]: 菜单栏中[File]->[Reload As E ...
- js url传值中文乱码完美解决(JAVA)
js url传值中文乱码完美解决(JAVA) 首先在你的jsp页面这样更改: var url="你要传入的Action的位置&ipid="+ipid+"& ...
随机推荐
- for循环取出每个i的值
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- JavaScript实现无缝滚动 原理详细讲解
先了解一下对象的几个的属性: innerHTML: 设置或获取位于对象起始和结束标签内的 HTML scrollHeight: 获取对象的滚动高度. scrollLeft: 设置或获取位于对象左边界和 ...
- 模拟21 题解(waiting)
留坑待填 效率!!! 题还没改Oh,NO!!!
- jquery源码学习(二)——jquery中的变量
jquery在 21-93 行提供了变量 var // A central reference to the root jQuery(document) rootjQuery, // The defe ...
- 微信小程序制作选项卡
wxml: <view class="tab"><view class="tab-title" bindtap="tabFun&qu ...
- using namespace std 和 include 的区别
using namespace std; int main() { std::string dd = "ddd";//会报错namespace "std" h ...
- python 基本数据结构 ndarray
- VirtualBox安装,VirtualBox安装CentOS
1.进入VirtualBox官网下载页,找到对应的版本 https://www.virtualbox.org/wiki/Downloads 按步骤安装好 2.进入CentOS官网下载页,找到对应的版本 ...
- iscroll5在使用情况下click事件失效的问题
转载自:http://www.52html5.com/?p=2618 Bug描述: iOS.android4.4+下不能触发click事件. Bug解决: 调用iscroll插件,增加配置参数:cli ...
- Linux C socket 基于 UDP
/************************************************************************* > File Name: serve ...