Interview knowledge-NiO

Source: Internet
Author: User

Most of the content is taken from thinking in Java .. Baodian-grade books

  1. Package NiO;
  2. Import java. Io. fileinputstream;
  3. Import java. Io. fileoutputstream;
  4. Import java. Io. randomaccessfile;
  5. Import java. NiO. bytebuffer;
  6. Import java. NiO. channels. filechannel;
  7. /**
  8. * The NiO speed is improved because the structure closer to the operating system I/O processing mode is used: channels and buffers.
  9. * If the channel is compared to a corner, the buffer is a hoe.
  10. * We will not directly move the corners. However, you only need to throw your hair ......
  11. *
  12. * There is only one minimum implementation type for dealing directly with channels: bytebuffer
  13. * 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)
  14. * The input and output functions cannot even directly input an object or string.
  15. *
  16. * The channel function is very simple: it processes bytebuffer read/write and locks the file block content.
  17. * The channel constructor is used to modify three byte-based objects in the original I/O system:
  18. * Fileinputstream, fileoutputstream, randomaccessfile
  19. *
  20. * @ Author Wei. songw
  21. * Oct 16,200 8 3:54:16
  22. */
  23. Public class basic {
  24. Private Static final int bsize = 1024;
  25. Public static void main (string [] ARGs) throws exception {
  26. // Write File
  27. Filechannel fc = new fileoutputstream ("abc.txt"). getchannel ();
  28. FC. Write (bytebuffer. Wrap ("some string". getbytes ()));
  29. FC. Close ();
  30. Fc = new randomaccessfile ("abc.txt", "RW"). getchannel ();
  31. FC. Position (FC. Size (); // go the end
  32. FC. Write (bytebuffer. Wrap ("more string". getbytes ()));
  33. FC. Close ();
  34. Fc = new fileinputstream ("abc.txt"). getchannel ();
  35. Bytebuffer BF = bytebuffer. Allocate (bsize );
  36. FC. Read (BF );
  37. BF. Flip (); // notice here. Flip Filter !!
  38. While (BF. hasremaining ()){
  39. System. Out. Print (char) BF. Get ());
  40. }
  41. }
  42. }

  1. Package NiO;
  2. Import java. NiO. bytebuffer;
  3. Import java. NiO. intbuffer;
  4. /**
  5. * Although bytebuffer only holds byte arrays, Apis provide some methods to handle primitives.
  6. * (Providing corresponding operations for objects is often a good OO design concept.
  7. * For example, if collection is provided, you must provide an iterator traversal instead of letting the user
  8. * To implement iteration or determine whether the dependency is out of bounds due to exceptions)
  9. *
  10. * (It is also a good habit to provide APIs with consistent structures and function naming rules .. Ascharbuffer,
  11. * Asintbuffer, asdoublebuffer, getint, getlong ....)
  12. * @ Author Wei. songw
  13. * Oct 16,200 8 8:19:49
  14. */
  15. Public class getdata {
  16. Private Static final int bsize = 1024;
  17. Public static void main (string [] ARGs) throws exception {
  18. Bytebuffer BB = bytebuffer. Allocate (bsize); // filled by 0
  19. // Char
  20. BB. ascharbuffer (). Put ("Heihei ");
  21. Char C;
  22. While (C = BB. getchar ())! = 0 ){
  23. System. Out. Print (C + "");
  24. }
  25. BB. Rewind ();
  26. // Int
  27. BB. asintbuffer (). Put (12345 );
  28. System. Out. Print (BB. getint ());
  29. // Batches
  30. BB. Clear ();
  31. Intbuffer IB = BB. asintbuffer ();
  32. Ib. Put (New int [] {11,22, 33,44, 55 });
  33. System. Out. println (IB. Get (3 ));
  34. }
  35. }

  1. Package NiO;
  2. Import java. Io. randomaccessfile;
  3. Import java. NiO. mappedbytebuffer;
  4. Import java. NiO. channels. filechannel;
  5. /**
  6. * In the online copy example, the memory ing file is mainly used to operate large files. The maximum size is 2 GB.
  7. * @ Author Wei. songw
  8. * Oct 16,200 8 10:34:52
  9. */
  10. Public class memorymappedfile {
  11. Static int length = 0x8ffffff; // 128 MB
  12. Public static void main (string [] ARGs) throws exception {
  13. Mappedbytebuffer out =
  14. New randomaccessfile ("test. dat", "RW"). getchannel ()
  15. . Map (filechannel. mapmode. read_write, 0, length );
  16. For (INT I = 0; I <length; I ++)
  17. Out. Put (byte) 'X ');
  18. System. Out. println ("finished writing ");
  19. For (INT I = length/2; I <length/2 + 6; I ++)
  20. System. Out. Print (char) Out. Get (I); // Read File
  21. }
  22. }
  1. Package NiO;
  2. Import java. Io. fileoutputstream;
  3. Import java. NiO. channels. filelock;
  4. Import java. util. Concurrent. timeunit;
  5. /**
  6. * It mainly studies the APIs of trylock () and lock.
  7. * Full-text locking without parameters supports changing the file size,
  8. * Partial locking with a fixed size will not automatically adapt to file growth (
  9. * If a locked region initially contains the end Of the file
  10. * And the file grows beyond the region then the new portion of the file
  11. * Will not be covered by the lock .)
  12. *
  13. *
  14. * File locks also support memorymappedfile.
  15. *
  16. * @ Author Wei. songw
  17. * Oct 16,200 8 11:16:14
  18. */
  19. Public class filelocking {
  20. Public static void main (string [] ARGs) throws exception {
  21. Fileoutputstream Fos = new fileoutputstream ("abc.txt ");
  22. // Locking the hole File
  23. Filelock FL = FOS. getchannel (). trylock ();
  24. If (FL! = NULL ){
  25. System. Out. println ("file locked ");
  26. // Do your work
  27. Timeunit. milliseconds. Sleep (2000 );
  28. Fl. Release ();
  29. System. Out. println ("released lock ");
  30. }
  31. // For locking a part of the file, use:
  32. // Filelock trylock (long position, long size, Boolean shared)
  33. // Filelock lock (long position, long size, Boolean shared)
  34. FOS. Close ();
  35. }
  36. }

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.