用nio中的CharBuffer(string str)写入文件中文显示不出来,是unicode的问题吗 如何解决?谢谢!!

HuoChunGuang 2008-02-19 11:20:00
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.channels.FileChannel;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.io.IOException;

public class writefile {

/**
* @param args
*/

public static void main(String[] args) {
// TODO Auto-generated method stub
File file=new File("test.txt");
FileOutputStream outputFile=null;
try{
outputFile=new FileOutputStream(file);
}catch(FileNotFoundException fnfe){
fnfe.printStackTrace(System.err);
System.exit(1);
}
FileChannel outputChannel=outputFile.getChannel();
String str="wellcome to china,欢迎来到中国。";
ByteBuffer buff=ByteBuffer.allocate(50);
CharBuffer cbuff=buff.asCharBuffer();
cbuff.put(str);
buff.position(cbuff.position()*2);
buff.flip();
try{
outputChannel.write(buff);
}catch(IOException ioe){
ioe.printStackTrace(System.err);
}

}

}
用nio中的CharBuffer(string str)写入文件中文显示不出来,是unicode的问题吗 如何解决?谢谢!!
我是用eclipse编译的,用bytebuffer可以实现,我就是想用一下charbuffer视图缓冲区写一下。
...全文
425 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧