NIo写字符到文件
package test; import jdk.nashorn.internal.ir.CallNode; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; /** * 使用NIO输出文字到文本中 */ public class Niodemo { public static void main(String[] args) throws Exception { String s="Nio 学习"; //创建输出流 FileOutputStream fileOutputStream = new FileOutputStream("d:\\nio.txt"); //获取fileChannel FileChannel channel = fileOutputStream.getChannel(); //创建BUffer ByteBuffer byteBuffer = ByteBuffer.allocate(1024); //将s字符串存入Buffer byteBuffer.put(s.getBytes()); //byteBuffer 反转 byteBuffer.flip(); //将byteBuffer 写入fileChannel channel.write(byteBuffer); //关闭流 fileOutputStream.close(); } }
NiO

浙公网安备 33010602011771号