1. I spoke to you about Java's Io features yesterday. Now I should be able to read and write text files and binary files. But in some special cases, we need faster Io operations, let's talk about Java. NIO. * package; 2. for faster I/O operations, we usually use a structure similar to the way in which the operating system executes IO: channels and buffers; we do not directly deal with channels, but we deal with channels through buffers. Therefore, the channel either obtains data from the buffer or sends data to the buffer. the only buffer that can be used directly with the channel is bytebuffer (you can view the Java Doc). The following describes the sample program, which implements file read/write and random read/write package myjava through the file channel and bytebuffer. base; import Java. io. *;
Import java. NiO .*;
Import java. NiO. channels. *; public class channeldemo
{
Public static void main (string [] ARGs) throws ioexception
{
// Obtain the file channel,
Filechannel fc = new fileoutputstream ("E: // Java // jcreator2.5 // encryptor. txt"). getchannel ();
// Write data
FC. Write (bytebuffer. Wrap ("some text". getbytes ()));
FC. Close ();
// Random read/write
Fc = new randomaccessfile ("E: // Java // jcreator2.5 // encryption and decryption .txt", "RW"). getchannel ();
FC. Position (FC. Size ());
FC. Write (bytebuffer. Wrap ("some more text". getbytes ()));
FC. Close ();
Fc = new fileinputstream ("E: // Java // jcreator2.5 // encryptor .txt"). getchannel ();
Bytebuffer buff = bytebuffer. Allocate (1024 );
FC. Read (buff );
Buff. Flip (); // indicates that the channel is ready and can be read and written.
While (buff. hasremaining ())
{
System. Out. println (char) buff. Get ());
}
} 4. Copy package myjava. Base by connecting two channels; import java. NiO .*;
Import java. Io .*;
Import java. NiO. channels. *; public class copybyconnectchanneldemo
{
Public static void main (string [] ARGs) throws ioexception
{
Filechannel srcfile = new fileinputstream ("E: // Java // jcreator2.5 // encryptor. txt"). getchannel ();
Filechannel destfile = new fileoutputstream ("E: // Java // jcreator2.5 // encryption (copypolic.txt"). getchannel ();
}
} 5. the bytebuffer basic data type is bytebuffer. What if we want to insert or extract basic data to bytebuffer? The solution is to extract: use bytebuffer's getint () or getfloat () methods; insert: Use bytebuffer's ascharbuffer (). put () and other methods; 6. the role of view buffer is to create different view buffer charbuffer cb = (bytebuffer) BB through the same bytebuffer. rewind ()). ascharbuffer (); 7. memory ing file (1 ). why do I need a memory ing file ?? The memory ing file allows us to create and modify files that are too large to be stored in the memory. With the memory ing file, we can assume that the entire file is stored in the memory, it can be completely accessed as a very large array. (2 ). using memory ing files can significantly speed up (3) using mappedbytebuffer for memory ing files; (4 ). example: create a large file channel and read and write the file package myjava. base; import Java. io. *;
Import java. NiO .*;
Import java. NiO. channels. *; public class mappedfiledemo
{
Static int length = 0 xffffff;
Public static void main (string [] ARGs) throws ioexception
{
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