首页 > 编程语言 > 详细

Java 异步NIO写文件,无队列线程池方案

时间:2018-03-01 15:58:42      阅读:207      评论:0      收藏:0      [点我收藏+]

java.nio.channels.AsynchronousChannel提供了异步写文件方法,

具体代码如下:

技术分享图片
public static void syncWrite(String path){
    File file = new File(path+"warn.log");
            if(!file.exists()) {
                file.createNewFile();
            }
            Path pathSyn = Paths.get(path+line+"warn.log");
            try {
                AsynchronousFileChannel channel = AsynchronousFileChannel.open(pathSyn, StandardOpenOption.WRITE);
                ByteBuffer buffer = ByteBuffer.allocate(1024);
                buffer = ByteBuffer.wrap(clientSender.getBytes("utf-8"));
                Future<Integer> future = channel.write(buffer, channel.size());
                channel.force(true);
                while (!future.isDone());
                buffer.clear();
                channel.close();
            } catch (Exception e) {
                e.printStackTrace();
            }    
}
异步NIO写文件

 

Java 异步NIO写文件,无队列线程池方案

原文:https://www.cnblogs.com/codegod/p/8488389.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!