-------------------------- FileChannel | -------------------------- # Java NIOÖеÄFileChannelÊÇÒ»¸öÁ¬½Óµ½ÎļþµÄͨµÀ¡£¿ÉÒÔͨ¹ýÎļþͨµÀ¶ÁдÎļþ¡£ # FileChannelÎÞ·¨ÉèÖÃΪ·Ç×èÈûģʽ£¬Ëü×ÜÊÇÔËÐÐÔÚ'×èÈûģʽÏÂ' # »ñÈ¡ FileChannel * FileChannel ¾²Ì¬·½·¨ FileChannel.open(Path path, OpenOption... options); FileChannel.open(Path path, Set extends OpenOption> options,FileAttribute>... attrs) * Á÷¶ÔÏóµÄ·½·¨ * InputStream,OutputStream,RandomAccessFile * getChannel(); ·½·¨,×ÜÊÇ´æÔÚÕâЩÁ÷ÀïÃæ # ÔÚʹÓà FileChannel ֮ǰ£¬±ØÐëÏÈ´ò¿ªËü¡£ÐèҪͨ¹ýʹÓÃÒ»¸ö InputStream , OutputStream »ò RandomAccessFile À´»ñȡһ¸öFileChannelʵÀý * getChannel(); ·½·¨,×ÜÊÇ´æÔÚÕâЩÁ÷ÀïÃæ RandomAccessFile file = new RandomAccessFile("E:\\Main.java", "rw"); FileChannel fromChanne = file.getChannel(); * ´Ó InputStream »ñÈ¡ Channel ReadableByteChannel readableChannel = Channels.newChannel(InputStream in); * ´Ó OutputStream »ñÈ¡ Channel # ´Ó FileChannel ÖжÁÈ¡Êý¾Ý ByteBuffer buf = ByteBuffer.allocate(48); //×¼±¸Ò»¸ö48×Ö½ÚµÄbuffer int bytesRead = inChannel.read(buf); //°ÑͨµÀÖеÄÊý¾Ý¶Áµ½bufferÖÐ *¡¡read()·½·¨·µ»ØµÄintÖµ±íʾÁËÓжàÉÙ×Ö½Ú±»¶Áµ½ÁËBufferÖС£Èç¹û·µ»Ø-1£¬±íʾµ½ÁËÎļþĩβ¡£ # Ïò FileChannel дÊý¾Ý String newData = "New String to write to file..." + System.currentTimeMillis(); ByteBuffer buf = ByteBuffer.allocate(48); buf.clear(); //ÖØÖÃbuffer,Ö¸ÕëÒÆ¶¯µ½0 buf.put(newData.getBytes()); //Ìî³äÊý¾Ý buf.flip(); //Ð޸ĶÁ/дģʽ,Ö¸ÕëÒÆµ½0 while(buf.hasRemaining()) { channel.write(buf); } * ʹÓÃFileChannel.write()·½·¨ÏòFileChannelдÊý¾Ý£¬¸Ã·½·¨µÄ²ÎÊýÊÇÒ»¸öBuffer * ×¢ÒâFileChannel.write()ÊÇÔÚwhileÑ»·Öе÷Óõġ£ * ÒòΪÎÞ·¨±£Ö¤write()·½·¨Ò»´ÎÄÜÏòFileChannelдÈë¶àÉÙ×Ö½Ú£¬Òò´ËÐèÒªÖØ¸´µ÷ÓÃwrite()·½·¨£¬Ö±µ½BufferÖÐÒѾûÓÐÉÐδдÈëͨµÀµÄ×Ö½Ú¡£ # ¹Ø±Õ FileChannel channel.close(); * ÓÃÍêFileChannelºó±ØÐ뽫Æä¹Ø±Õ # ÑÝʾһ¸ö´ÓÎļþÖлñÈ¡ Channel µÄʵÀý RandomAccessFile file = new RandomAccessFile("E:\\Main.java","rw"); FileChannel fileChannel = file.getChannel(); //´ÓÎļþ»ñÈ¡Á÷ ByteBuffer byteBuffer = ByteBuffer.allocate(1024); //¹¹½¨1MBµÄ»º³åÇø int len = fileChannel.read(byteBuffer); //ͨ¹ý¹ÜµÀ°ÑÊý¾ÝдÈ뻺³åÇø while (len != -1){ byteBuffer.flip(); //¸´Î»,¿ªÊ¼¶ÁÈ¡Êý¾Ý while (byteBuffer.hasRemaining()){ System.out.print((char) byteBuffer.get()); } byteBuffer.clear(); //Êý¾Ý¶ÁÈ¡Íê±Ï,¸´Î»¸Ã»º³åÇø×¼±¸Ð´Èë len = fileChannel.read(byteBuffer); //ͨ¹ý¹ÜµÀ°ÑÊý¾ÝдÈ뻺³åÇø } file.close(); //¹Ø±Õ RandomAccessFile »á¹Ø±Õ¶ÔÓ¦µÄ FileChannel ------------------------------- scatter | ------------------------------- # Scattering ReadsÊÇÖ¸Êý¾Ý´ÓÒ»¸öchannel¶ÁÈ¡µ½¶à¸öbufferÖС£ÈçÏÂͼÃèÊö£º |-> Buffer Channel -> |-> Buffer |-> Buffer # read()·½·¨°´ÕÕbufferÔÚÊý×éÖеÄ˳Ðò½«´ÓchannelÖжÁÈ¡µÄÊý¾ÝдÈëµ½buffer£¬µ±Ò»¸öbuffer±»Ð´Âúºó£¬channel½ô½Ó×ÅÏòÁíÒ»¸öbufferÖÐд¡£ # Scattering Reads'ÔÚÒÆ¶¯ÏÂÒ»¸öbufferǰ£¬±ØÐëÌîÂúµ±Ç°µÄbuffer'£¬ÕâÒ²Òâζ×ÅËü²»ÊÊÓÃÓÚ¶¯Ì¬ÏûÏ¢(ÒëÕß×¢£ºÏûÏ¢´óС²»¹Ì¶¨)¡£ # »»¾ä»°Ëµ£¬Èç¹û´æÔÚÏûϢͷºÍÏûÏ¢Ì壬ÏûϢͷ±ØÐëÍê³ÉÌî³ä£¨ÀýÈç 128byte£©£¬Scattering Reads²ÅÄÜÕý³£¹¤×÷¡£ # ´úÂëDemo ByteBuffer header = ByteBuffer.allocate(128); //Ò»¸öBuffere ByteBuffer body = ByteBuffer.allocate(1024); //ÓÖÒ»¸öBuffere ByteBuffer[] bufferArray = { header, body }; //°ÑËùÓÐBufferºÏ²¢ÎªÒ»¸öBufferÊý×é channel.read(bufferArray); //´ÓChannleÖжÁÈ¡Êý¾Ýµ½ÕâЩBuffer ------------------------------- gather | ------------------------------- # Gathering WritesÊÇÖ¸Êý¾Ý´Ó¶à¸öbufferдÈ뵽ͬһ¸öchannel¡£ÈçÏÂͼÃèÊö£º <-| Buffer Channel <- <-| Buffer <-| Buffer # write()·½·¨»á°´ÕÕbufferÔÚÊý×éÖеÄ˳Ðò£¬½«Êý¾ÝдÈëµ½channel£¬×¢ÒâÖ»ÓÐpositionºÍlimitÖ®¼äµÄÊý¾Ý²Å»á±»Ð´Èë¡£ # Èç¹ûÒ»¸öbufferµÄÈÝÁ¿Îª128byte£¬µ«Êǽö½ö°üº¬58byteµÄÊý¾Ý£¬ÄÇôÕâ58byteµÄÊý¾Ý½«±»Ð´Èëµ½channelÖС£ # Òò´ËÓëScattering ReadsÏà·´£¬Gathering WritesÄܽϺõĴ¦Àí¶¯Ì¬ÏûÏ¢¡£ # ´úÂëDemo ByteBuffer header = ByteBuffer.allocate(128); //Ò»¸öBuffer ByteBuffer body = ByteBuffer.allocate(1024); //ÓÖÒ»¸öBuffer ByteBuffer[] bufferArray = { header, body }; //°ÑËùÓÐBufferºÏ²¢ÎªÒ»¸öÊý×é channel.write(bufferArray); //°ÑÊý¾Ý´ÓBuffreÖÐдÈëµ½channel ------------------------------- ͨµÀÖ®¼äµÄÊý¾Ý´«Êä | ------------------------------- # Èç¹ûÁ½¸öͨµÀÖÐÓÐÒ»¸öÊÇ FileChannel £¬ÄÇÄã¿ÉÒÔÖ±½Ó½«Êý¾Ý´ÓÒ»¸öchannel,´«Êäµ½ÁíÍâÒ»¸öchannel¡£ # °ÑÄ¿±ê Channel µÄÊý¾Ý¶ÁÈ¡µ½ this Channel ÖÐ * Demo RandomAccessFile fromFile = new RandomAccessFile("E:\\Main.java", "rw"); FileChannel fromChannel = fromFile.getChannel(); //Ô´Chnnel RandomAccessFile toFile = new RandomAccessFile("E:\\Main1.java", "rw"); FileChannel toChannel = toFile.getChannel(); //Ä¿±êChnnel long position = 0; //Ö¸ÕëΪ0 long count = fromChannel.size(); //»ñÈ¡µÄÊÇÔ´channelµÄ×Ü´óСµÄÊý¾ÝÁ¿ toChannel.transferFrom(fromChannel, position, count); //Ö´ÐÐÊôÐÔ´«Êä * ¹ûԴͨµÀµÄÊ£Óà¿Õ¼äСÓÚ count ¸ö×Ö½Ú£¬ÔòËù´«ÊäµÄ×Ö½ÚÊýҪСÓÚÇëÇóµÄ×Ö½ÚÊý¡£ * ÔÚSoketChannelµÄʵÏÖÖУ¬'SocketChannelÖ»»á´«Êä´Ë¿Ì×¼±¸ºÃµÄÊý¾Ý(¿ÉÄܲ»×ãcount×Ö½Ú)'¡£ //SocketChannelÊÇÍøÂç * Òò´Ë,SocketChannel ¿ÉÄܲ»»á½«ÇëÇóµÄËùÓÐÊý¾Ý(count¸ö×Ö½Ú) È«²¿´«Êäµ½FileChannelÖС£ # °Ñ this Channel ÖеÄÊý¾ÝдÈëµ½ Ä¿±ê Channel ÖÐ * Demo RandomAccessFile fromFile = new RandomAccessFile("E:\\Main.java", "rw"); FileChannel fromChanne = fromFile.getChannel(); RandomAccessFile toFile = new RandomAccessFile("E:\\ooooo.java", "rw"); FileChannel toChannel = toFile.getChannel(); long position = 0; long count = fromChanne.size(); fromChanne.transferTo(position, count, toChannel); * ³ýÁ˵÷Ó÷½·¨µÄFileChannel¶ÔÏó²»Ò»ÑùÍ⣬ÆäËûµÄ¶¼Ò»Ñù¡£ * ¹ØÓÚSocketChannelµÄÎÊÌâÔÚtransferTo()·½·¨ÖÐͬÑù´æÔÚ¡£ * 'SocketChannel»áÒ»Ö±´«ÊäÊý¾ÝÖ±µ½Ä¿±êbuffer±»ÌîÂú'¡£ ------------------------------- FileChannel-ÄÚ´æÎļþÓ³Éä | ------------------------------- # MappedByteBuffer map(MapMode mode,long position, long size); ------------------------------- FileChannel-API | ------------------------------- int read(ByteBuffer buffer); * ½«Êý¾Ý´Ó FileChannel ¶ÁÈ¡µ½BufferÖС£ * read()·½·¨·µ»ØµÄintÖµ±íʾÁËÓжàÉÙ×Ö½Ú±»¶Áµ½ÁËBufferÖС£Èç¹û·µ»Ø-1£¬±íʾµ½ÁËÎļþĩβ¡£ int write(ByteBuffer src); * °Ñ src ÖеÄÊý¾ÝдÈëµ½ FileChannel ÖÐ long transferFrom(ReadableByteChannel src,long position, long count); * °Ñ src ͨµÀÖеÄÊý¾ÝдÈëµ½ this ͨµÀ,´Ó position ¿ªÊ¼Ð´,д count ³¤¶È * ·µ»Ø long,±íʾдÈëÁ˶àÉÙÊý¾Ý long transferTo(ReadableByteChannel src,long position, long count); * °Ñ this ͨµÀÖеÄÊý¾ÝдÈëµ½ src ͨµÀ,´Ó position ¿ªÊ¼Ð´,д count ³¤¶È * ·µ»Ø long,±íʾдÈëÁ˶àÉÙÊý¾Ý long position(); * »ñÈ¡FileChannelµÄµ±Ç°Î»Öà * Èç¹û½«Î»ÖÃÉèÖÃÔÚÎļþ½áÊø·ûÖ®ºó£¬È»ºóÊÔͼ´ÓÎļþͨµÀÖжÁÈ¡Êý¾Ý£¬¶Á·½·¨½«·µ»Ø-1 ¡ª¡ª Îļþ½áÊø±êÖ¾¡£ void position(long position); * ÉèÖÃFileChannelµÄλÖà long size(); * ·µ»Ø¹ØÁªÎļþµÄ´óС FileChannel truncate(long size); * ½ØÈ¡Ò»¸öÎļþ¡£½ØÈ¡Îļþʱ£¬Îļþ½«ÖÐÖ¸¶¨³¤¶ÈºóÃæµÄ²¿·Ö½«±»É¾³ý * size(×Ö½Ú)¶à´ó,¾Í½ØÈ¡¶à´ó,ºóÃæµÄ¶¼²»Òª void force(boolean flag); * ½«Í¨µÀÀïÉÐδдÈë´ÅÅ̵ÄÊý¾ÝÇ¿ÖÆÐ´µ½´ÅÅÌÉÏ * ³öÓÚÐÔÄÜ·½ÃæµÄ¿¼ÂÇ£¬²Ù×÷ϵͳ»á½«Êý¾Ý»º´æÔÚÄÚ´æÖУ¬ËùÒÔÎÞ·¨±£Ö¤Ð´Èëµ½FileChannelÀïµÄÊý¾ÝÒ»¶¨»á¼´Ê±Ð´µ½´ÅÅÌÉÏ¡£Òª±£Ö¤ÕâÒ»µã£¬ÐèÒªµ÷ÓÃforce()·½·¨¡£ * booleanÀàÐ͵IJÎÊý£¬Ö¸Ã÷ÊÇ·ñͬʱ½«ÎļþÔªÊý¾Ý£¨È¨ÏÞÐÅÏ¢µÈ£©Ð´µ½´ÅÅÌÉÏ¡£ channel.close(); * ¹Ø±Õ Channel FileLock fileChannel.tryLock(); * ¸Ãµ÷Óò»»á×èÈû,Ö±½Ó»ñÈ¡Ëø,Èç¹ûËø²»´æÔÚ,·µ»Ønull * »ñÈ¡ÎļþËø * demo FileLock lock = fileChanne.lock(); //»ñÈ¡ÎļþËø lock.release(); //ÊÍ·ÅËø FileLock fileChannel.lock(); * ͬÉÏ * ¸Ãµ÷Óûá×èÈû,Ö±µ½»ñÈ¡µ½Ëø MappedByteBuffer map(MapMode mode,long position, long size) * °Ñµ±Ç°ChannelÓ³Éäµ½ÄÚ´æ * mode Ö¸¶¨·½Ê½(Ö»¶Á,¿Éд....),position Ö¸¶¨Îļþ¹ÜµÀµÄ¿ªÊ¼Î»ÖÃ,size Ö¸¶¨½áÊøÎ»Öà * MappedByteBuffer ÊÇ ByteBuffer µÄ×ÓÀà ------------------------------- FileChannel-ÄÚ²¿¾²Ì¬Àà | ------------------------------- MapMode READ_ONLY * Ö»¶Á,Èç¹ûд²Ù×÷»áÒì³£ READ_WRITE * ¿Éд,ÈκÎÐ޸ͼ»áÔÚij¸öʱ¼ä±»Ð´ÈëÎļþϵͳ PRIVATE * ¿Éд,µ«ÊÇÕâ¸ö²»»áдÈëµ½Îļþϵͳ