Using nio.2 to manipulate the file system

Source: Internet
Author: User

Java has been to JDK 1.3, is the use of java.io under the category of I/O, for this interested people, can take the "java i/o", that is I tidied up 13 years ago ... Xd. After JDK 1.4 was provided NIO, to JDK 1.7 plus some new features, called Nio.2, these categories are placed under the Java.nio, generally speaking, the use of Java.nio-type operation of the file system is more efficient than the use of java.io efficiency is high and convenient.

Before entering the subject, you might as well take a look at my previous "working with the Path class" and "metadata File attributes", which is a front-end dish that starts with explaining how to start, read, and write.

    • File

Whether you want to read a file or a file, the first step is to open the file, Java.nio provides the following ways to start:

    1. READ: To access the file's contents
    2. Write: To write information about a file
    3. Create: Create a new file and, if it exists, delete it and re-establish the new file.
    4. Create_new: Create a new file, and when the file is already there, throw out the exception.
    5. APPEND: Add content to an existing file.
    6. Delete_on_close: This option is used on a temporary file, and when the file is closed, delete the file.
    7. Truncate_existing: Clear the file and then start writing.
    8. SPARSE:
    9. SYNC: Keep the file content and metadata intact.
    10. Dsync: Keep your files intact.

Below is one of the most typical examples:

1  PackageIdv.steven.nio2.metadata;2 3 Importjava.io.IOException;4 ImportJava.nio.channels.ReadableByteChannel;5 ImportJava.nio.file.Files;6 ImportJava.nio.file.Path;7 Importjava.nio.file.Paths;8 Importjava.nio.file.StandardOpenOption;9 Ten  Public classNio2file { One      Public Static voidMain (string[] args)throwsIOException { APath PATH = Paths.get ("C:/java/poi-3.11/notice"); -         ReadablebytechannelChannel =files.newbytechannel (Path, standardopenoption.  READ); -         //... the     } -}

In java.io, I/O is manipulated in streaming (stream), and the Java.nio is changed to channel, the above program is to open a read file, all the file mode is defined in standopenoption this self-stereotypes. If you want to open a file that is written in, the 13th line may be changed to the following:

Writablebytechannel channel =newopenoption[] {standardopenoption.  CREATE, standardopenoption.write});

It is not possible to write this only, but also to add the parameters such as CREATE, Create_new, and all the parameters in a openoption array.

    • Channels (channel)

Java.nio define the channel? As shown in the following category:

The diagram lists only the commonly used interfaces and their methods, and to learn more about the types of inheritance, see JDK Doc. Readablebytechannel, Writablebytechannel and Seekablebytechannel are most commonly used when working with files, and the first two are used for reading and writing, and the third is the same time used to read and write channels, And you can move the file's target in the read-write process. As Networkchannel and Multicastchannel are used on the TCP and UDP routes of the network, they are also stated.

In Java.nio, the channel is replaced by the stream in Java.io, the same time the channel operation of the object is not byte[], char[] ... Change to Java.nio self-defined bytebuffer, Charbuffer and other categories, the most commonly used is bytebuffer, about Bytebuffer, please see the "bytebuffer the Mark said," before continuing to look down, please first understand Bytebuffer.

    • Your partner
1  PackageIdv.steven.nio2.metadata;2 3 Importjava.io.IOException;4 ImportJava.nio.ByteBuffer;5 ImportJava.nio.channels.ReadableByteChannel;6 ImportJava.nio.file.Files;7 ImportJava.nio.file.Path;8 Importjava.nio.file.Paths;9 Importjava.nio.file.StandardOpenOption;Ten  One  Public classNio2file { A      Public Static voidMain (string[] args)throwsIOException { -Path PATH = Paths.get ("C:/java/poi-3.11/notice"); -Readablebytechannel channel =files.newbytechannel (path, standardopenoption.read); the          -Bytebuffer buffer = bytebuffer.allocate (1024); -          -          while(channel.Read(buffer) > 0) { +System.out.println (NewString (Buffer.array ())); - Buffer.flip (); +         } A          at channel.close (); -     } -}

The above program reads 1024 byte at 18 lines at a time until the end of the file, and then it is inserted into buffer, and in 19 rows the contents of the reading will be output, and then the file (23 lines) should be remembered.

    • Archive
1  PackageIdv.steven.nio2.metadata;2 3 Importjava.io.IOException;4 ImportJava.nio.ByteBuffer;5 ImportJava.nio.channels.ReadableByteChannel;6 ImportJava.nio.channels.WritableByteChannel;7 ImportJava.nio.file.Files;8 Importjava.nio.file.OpenOption;9 ImportJava.nio.file.Path;Ten Importjava.nio.file.Paths; One Importjava.nio.file.StandardOpenOption; A  -  Public classNio2file { -      Public Static voidMain (string[] args)throwsIOException { thePath PATH = Paths.get ("C:/java/poi-3.11/notice"); -Readablebytechannel channel =files.newbytechannel (path, standardopenoption.read); -          -Path Pathto = Paths.get ("C:/java/poi-3.11/notice.txt"); +         Writablebytechannel Channelto = Files.newbytechannel (Pathto, -                 New openoption[] {standardopenoption.create, standardopenoption.write}); +          ABytebuffer buffer = bytebuffer.allocate (1024); at          while((channel.read (buffer)) > 0) { - Buffer.flip (); - channelto.write (buffer); - Buffer.flip (); -         } -          in channel.close (); - channelto.close (); to     } +}

This program simply NOTICE the last program, writes the contents of the reader to the NOTICE.txt file, and opens a written file in the 19~20 line, and writes the contents to the specified file in the 24~26 line.

Using nio.2 to manipulate the file system

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.