用nio中的CharBuffer(string str)写入文件中文显示不出来,是unicode的问题吗 如何解决?谢谢!!
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视图缓冲区写一下。