Flow and File: Introduction and use of nio.2

Source: Internet
Author: User
In traditional Java, there is only one file class, which represents a file and represents a directory. Java 7 adds the following APIs to access files
Path-Interface, representing a platform-independent directory. Provides a number of ways to manipulate the directory.
Paths-Tools class. All methods are static.
Files-the tool class for manipulating files. Provides a number of ways to manipulate files. The large number of methods contained in this class may be somewhat different from our usual expectations.

Earlier Java provided only the file class to access files, its limited functionality and low performance, NIO.2 provided the path interface and the paths and Files tool classes to access the file system.

1.Path Interface and Paths tool classJava.nio.file.Path
The path interface represents a platform-independent path.
An object of May is used to locate a file system. It would typically represent a system dependent file path.


Java.nio.file.Paths
This class consists exclusively of the static methods that return a path by converting a path string or URI.

part of the method introduction:
1.getNameCountint GetNameCount ()
Returns the number of paths contained in the current path object.
Returns the number of name elements in the path.
Returns the number of elements in the path, or 0 if this path is only represents a root component.
2.getRootPath Getroot ()
Gets the root path of the current path object
Returns The root component of this path as a path object, or null if this path does is not have a root component.
Returns:a path representing the root component of this path, or null.
3.toAbsolutePathPath Toabsolutepath ()
Gets the absolute path corresponding to the current path object.
Returns a Path object representing the absolute path of this path.
If This path was already absolute then this method simply returns this path.
Otherwise, this is resolves the path in a implementation dependent manner,
Typically by resolving the path against a file system default directory.
Depending on the implementation, this method may throw the I/O error if the file system is not accessible.
Returns:a Path object representing the absolute path.

2.Files Tool ClassThe tool class for manipulating files. Because of the powerful features of the Files tool class, you can use the files class to simplify file IO.
Java.nio.file.Files
Public final class Files extends Object
This class consists exclusively of the static methods that operate in files, directories, or other types of files.

In most cases, the methods defined here would delegate to the associated file system provider to perform the file operation S.

Here are some simple actions to demonstrate the power of NIO.

Package test.jse.io.nio2.demo1;
Import Java.io.FileOutputStream;
Import Java.nio.charset.Charset;
Import Java.nio.file.Files;
Import Java.nio.file.Path;
Import java.nio.file.Paths;

Import java.util.List;
		public class Nio2test {public static void main (string[] args) throws Exception {//testpath ();
	TestFiles ();
		public static void Testpath () {//creates the Path object path Path=paths.get (".") with the current path;
		System.out.println ("Number of paths included in the current path object:" +path.getnamecount ());
		System.out.println ("The root path of the current path object:" +path.getroot ());
		Gets the absolute path for path Absolutepath=path.toabsolutepath ();
		SYSTEM.OUT.PRINTLN (absolute path corresponding to path: "+absolutepath);"
	System.out.println ("Number of paths contained in the current Absolutepath object:" +absolutepath.getnamecount ()); public static void TestFiles () throws Exception {//Copy file Files.copy (Paths.get ("Src/test/jse/io/nio2/demo1/nio2test
		. Java "), New FileOutputStream (" Src/test/jse/io/nio2/demo1/new_nio2test.java ")); Determine if the Nio2test.java file is a hidden file System.out.println ("Nio2test.java file is hidden file: \ t" +fIles.ishidden (Paths.get ("Src/test/jse/io/nio2/demo1/nio2test.java")); Read all rows of Nio2test.java files at once list<string> Lines=files.readalllines (Paths.get ("src/test/jse/io/nio2/demo1/
		Nio2test.java "), Charset.forname (" GBK "));
		for (String line:lines) {System.out.println (line); }
	}
}

There are a lot of ways to simplify the operation of the flow and file in the API document, and I hope readers will try and summarize it and use it in the actual development.

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.