Most of the content is taken from thinking in Java .. Baodian-grade books
- Package NiO;
- Import java. Io. fileinputstream;
- Import java. Io. fileoutputstream;
- Import java. Io. randomaccessfile;
- Import java. NiO. bytebuffer;
- Import java. NiO. channels. filechannel;
- /**
- * The NiO speed is improved because the structure closer to the operating system I/O processing mode is used: channels and buffers.
- * If the channel is compared to a corner, the buffer is a hoe.
- * We will not directly move the corners. However, you only need to throw your hair ......
- *
- * There is only one minimum implementation type for dealing directly with channels: bytebuffer
- * During creation, you even need to specify the amount of memory space allocated. It only implements the byte format and the basic data format (primitive data)
- * The input and output functions cannot even directly input an object or string.
- *
- * The channel function is very simple: it processes bytebuffer read/write and locks the file block content.
- * The channel constructor is used to modify three byte-based objects in the original I/O system:
- * Fileinputstream, fileoutputstream, randomaccessfile
- *
- * @ Author Wei. songw
- * Oct 16,200 8 3:54:16
- */
- Public class basic {
- Private Static final int bsize = 1024;
- Public static void main (string [] ARGs) throws exception {
- // Write File
- Filechannel fc = new fileoutputstream ("abc.txt"). getchannel ();
- FC. Write (bytebuffer. Wrap ("some string". getbytes ()));
- FC. Close ();
- Fc = new randomaccessfile ("abc.txt", "RW"). getchannel ();
- FC. Position (FC. Size (); // go the end
- FC. Write (bytebuffer. Wrap ("more string". getbytes ()));
- FC. Close ();
- Fc = new fileinputstream ("abc.txt"). getchannel ();
- Bytebuffer BF = bytebuffer. Allocate (bsize );
- FC. Read (BF );
- BF. Flip (); // notice here. Flip Filter !!
- While (BF. hasremaining ()){
- System. Out. Print (char) BF. Get ());
- }
- }
- }
- Package NiO;
- Import java. NiO. bytebuffer;
- Import java. NiO. intbuffer;
- /**
- * Although bytebuffer only holds byte arrays, Apis provide some methods to handle primitives.
- * (Providing corresponding operations for objects is often a good OO design concept.
- * For example, if collection is provided, you must provide an iterator traversal instead of letting the user
- * To implement iteration or determine whether the dependency is out of bounds due to exceptions)
- *
- * (It is also a good habit to provide APIs with consistent structures and function naming rules .. Ascharbuffer,
- * Asintbuffer, asdoublebuffer, getint, getlong ....)
- * @ Author Wei. songw
- * Oct 16,200 8 8:19:49
- */
- Public class getdata {
- Private Static final int bsize = 1024;
- Public static void main (string [] ARGs) throws exception {
- Bytebuffer BB = bytebuffer. Allocate (bsize); // filled by 0
- // Char
- BB. ascharbuffer (). Put ("Heihei ");
- Char C;
- While (C = BB. getchar ())! = 0 ){
- System. Out. Print (C + "");
- }
- BB. Rewind ();
- // Int
- BB. asintbuffer (). Put (12345 );
- System. Out. Print (BB. getint ());
- // Batches
- BB. Clear ();
- Intbuffer IB = BB. asintbuffer ();
- Ib. Put (New int [] {11,22, 33,44, 55 });
- System. Out. println (IB. Get (3 ));
- }
- }
- Package NiO;
- Import java. Io. randomaccessfile;
- Import java. NiO. mappedbytebuffer;
- Import java. NiO. channels. filechannel;
- /**
- * In the online copy example, the memory ing file is mainly used to operate large files. The maximum size is 2 GB.
- * @ Author Wei. songw
- * Oct 16,200 8 10:34:52
- */
- Public class memorymappedfile {
- Static int length = 0x8ffffff; // 128 MB
- Public static void main (string [] ARGs) throws exception {
- Mappedbytebuffer out =
- New randomaccessfile ("test. dat", "RW"). getchannel ()
- . Map (filechannel. mapmode. read_write, 0, length );
- For (INT I = 0; I <length; I ++)
- Out. Put (byte) 'X ');
- System. Out. println ("finished writing ");
- For (INT I = length/2; I <length/2 + 6; I ++)
- System. Out. Print (char) Out. Get (I); // Read File
- }
- }
- Package NiO;
- Import java. Io. fileoutputstream;
- Import java. NiO. channels. filelock;
- Import java. util. Concurrent. timeunit;
- /**
- * It mainly studies the APIs of trylock () and lock.
- * Full-text locking without parameters supports changing the file size,
- * Partial locking with a fixed size will not automatically adapt to file growth (
- * If a locked region initially contains the end Of the file
- * And the file grows beyond the region then the new portion of the file
- * Will not be covered by the lock .)
- *
- *
- * File locks also support memorymappedfile.
- *
- * @ Author Wei. songw
- * Oct 16,200 8 11:16:14
- */
- Public class filelocking {
- Public static void main (string [] ARGs) throws exception {
- Fileoutputstream Fos = new fileoutputstream ("abc.txt ");
- // Locking the hole File
- Filelock FL = FOS. getchannel (). trylock ();
- If (FL! = NULL ){
- System. Out. println ("file locked ");
- // Do your work
- Timeunit. milliseconds. Sleep (2000 );
- Fl. Release ();
- System. Out. println ("released lock ");
- }
- // For locking a part of the file, use:
- // Filelock trylock (long position, long size, Boolean shared)
- // Filelock lock (long position, long size, Boolean shared)
- FOS. Close ();
- }
- }