Java NIO (quad) file channel

Source: Internet
Author: User

File Channel

The channel is the conduit that accesses the I/O service, and I/O can be divided into two broad categories: File I/O and stream I/O. Accordingly, there are two types of channels, which are file and socket (socket) channels. The file channel refers to the FileChannel, the socket channel has three, respectively is Socketchannel, Serversocketchannel and Datagramchannel

Channels can be created in a number of ways. The socket channel has a factory method (open () method) that directly creates the socket channel. a FileChannel object can only be obtained by invoking the Getchannel () method on an open randomaccessfile, FileInputStream, or FileOutputStream object. Developers cannot directly create a FileChannel

File channels are always blocked and cannot be placed in non-blocking mode

the FileChannel object is thread-safe. multiple processes can invoke methods concurrently on the same instance without causing any problems, but not all operations are multithreaded. Actions that affect the channel location or affect the file are single-threaded, and if one thread is already performing an operation that affects the channel location or file size, other threads that attempt one of these operations must wait, and the concurrency behavior is affected by the underlying operating system or file system.

Open FileChannel
New Randomaccessfile ("Data.txt", "rw"= Afile.getchannel ();
Reading data from FileChannel
Bytebuffer buf = bytebuffer.allocate (+); int bytr = Filechannel.read (BUF);

NOTE: The int value returned by the Read () method indicates how many bytes were read into buffer and, if 1, the end of the file

Writing data to FileChannel
1 String str = "some thing"; 2 Bytebuffer buf = bytebuffer.allocate (+); 3 buf.clear (); 4 Buf.put (Str.getbytes ()); 5 Buf.flip (); 6  while (Buf.hasremaining ()) {7  filechannel.write (BUF);       8 }
9 Filechannel.close ();

Note: The Write () method is in the while loop. Because there is no guarantee that write () can write as many bytes at a time, you need to repeat the call to know that there is no data in the buffer that has not yet been written to the channel

Close FileChannel
Filechannel.close ();

Position () method

Sometimes you may need to read and write data at a specific location in FileChannel, you can get the current position of FileChannel through the position () method, or through position (long newposition) Setting the location of the FileChannel

long position = channel.position (); Channel.position (position+10);

Note: If the position is set after the file terminator, then the read bytes will return-1, write the bytes, the file will expand to the current location, and the data will be written to the channel. This can result in a "file hole" where there is a gap between the data written in the physical file on the disk

Size () method
long size = Filechannel.size ();

Returns the file size associated with the instance

Truncate () method
Filechannel.truncate (1024);

This method intercepts a file, when the file is intercepted, the data after the specified part of the file is deleted, the above example indicates the first 1024 bytes of the intercepted file

Force () method
Filechannel.force (true);

This method forces data that has not yet been written to the disk in the channel to be written to disk. For performance reasons, the operating system caches data in memory, so there is no guarantee that data written to FileChannel will be written to disk immediately. To ensure this, the force () method needs to be called.

The Force () method has a Boolean parameter that indicates whether the file metadata (permission information, and so on) is written to disk at the same time.

Java NIO (quad) file channel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.