|
Creates a path instance
path instance contains information about the location of the specified file or directory, and when instantiating the path class, you need to specify one or more The directory or file name. The root directory of the path is not required; The path information may be just the name of a directory or file. The simplest way to create a Path instance is to use Paths (note that there is a get method for the S) class:
Path p1 = Paths.get ("/tmp/foo");
Path P2 = Paths.get (Args[0]);
Path p3 = paths.get ("File:///Users/joe/FileTest.java"); The
Path class accepts a string or URI as an argument.
Gets the path information
file system is generally a tree structure, so we can interpret path as a series of names (directory names and file names) that are stored sequentially. The highest directory name in the directory structure is the one that is index 0 in the sequence, and the lowest level of directory name or filename in the directory structure is the one in the sequence where Index is n-1 (where n is the number of levels in the path). The
path class provides methods to get an element or a subsequence from a sequence by index. |
|
Import java.nio.file.Paths;
Import Java.nio.file.Path;
public class test{public
static void Main (String args[]) {
Path Path = Paths.get ("C:\\home\\joe\\foo"); Microsoft Windows syntax
//path Path = Paths.get ("/home/joe/foo"); Solaris syntax
System.out.println ("path.tostring ()--" +path.tostring ());
System.out.println ("Path.getname (1)--" +path.getname (1));
System.out.println (path.getname (0));
System.out.println (Path.getnamecount ());
System.out.println (Path.subpath (0,2));
System.out.println (Path.getparent ());
System.out.println (Path.getroot ());
}
Run:
C:\ex>java Test
Path.tostring ()--c:\home\joe\foo
Path.getname (1)--joe
GetName (0): Home
Getnamecount:3
Home\joe
Getparent:c:\home\joe
Getroot:c:\
Java7 new feature IO operation path import Java.io.BufferedReader;
Import Java.io.File;
Import java.io.IOException;
Import Java.nio.charset.Charset;
Import Java.nio.file.FileSystems;
Import Java.nio.file.Files;
Import Java.nio.file.Path;
Import java.nio.file.Paths; /** * Path is an important entry point for file * operations in the java1.7 Nio.file package, and as a basis it is necessary to understand the * @author zKF57533/public class Testpath {Publi c static void Main (string[] args) {//Get path method One, c:/ex/access.log path Path = Filesystems.getdefault ()
. GetPath ("C:/ex", "Access.log");
System.out.println (Path.getnamecount ());
Obtain path method Two, using the Topath () method of file to obtain the path object file = New file ("E:/ex/access.log");
Path Pathother = File.topath ();
0, indicating that the two path is equal to System.out.println (Path.compareto (Pathother));
Get path Method Three path path3 = Paths.get ("C:/ex", "Access.log");
System.out.println (Path3.tostring ());
Join two paths Path Path4 = Paths.get ("C:/ex");
System.out.println ("Path4:" + path4.resolve ("Access.log"));
System.out.println ("--------------Split Line---------------");
try {if (files.isreadable (path)) {//Note the Newbufferedread charset parameter here, throw an exception if the encoding of the file you want to read is inconsistent Java's new feature, do not close the stream bufferedreader br = Files.newbufferedreader (path, Charset.defaul
Tcharset ());
New BufferedReader (New FileReader ("E:/logs/access.log"));//String line = "";
while (line = Br.readline ())!= null) {System.out.println (line);
}}else{System.err.println ("Cannot readable");
The catch (IOException e) {System.err.println ("error CharSet");
}
}
}Run:
C:\ex>java Testpath
2
-2
C:\ex\access.log
Path4:c:\ex\access.log
--------------Split Line---------------
Aa
Bb
Cc