package chapter1;import java.io.file;import java.io.ioexception;import java.nio.file.linkoption;import java.nio.file.path;import java.nio.file.paths;/** * Define Path Objects * * @author longlong * * @mail [email protected] * */public class pathtest { public Static void main (String[] args) { /** * 1. Defining an absolute path */ path path = paths.get ("c:/my/ Test1.log "); system.out.println (path); The //get method can be used to set parameters path = paths.get("C:", "my" , "Test1.log") ; System.out.println (PATH); /** * 2. Defining a relative path */ path = paths.get ("/ My " , " Test1.log ") ; system.out.println (path); /** * 3. Relative to working directory path */ path = paths.get ("My" , " Test1.log ") ; system.out.println (path); &nbSp; /** * 4. Shorthand for paths &NBSP;:&NBSP: Indicates the current directory .. Indicate parent directory */ Path = paths.get ("C:/my/./test1.log"). Normalize () ; &NBSP;SYSTEM.OUT.PRINTLN (path); path = paths.get ("c:/ My/./. /test1.log "). Normalize () ; system.out.println (path); /** * 5. definition Path can also use filesystems.getdefault ( ). GetPath (...) */ /** &nbSp; * 6. Get home Path */ path = paths.get (System.getProperty ("User.home"), "Downloads", "Game.exe"); system.out.println (path); /** * 7. After getting the path object, get more information about the content */ path = paths.get ("c:/my/ Test1.log "); // A. Get its file name system.out.println (Path.getfilename ()); //b. Get the root directory of a file &nBsp; system.out.println (Path.getroot ()); //c. Get the parent directory path of the file System.out.println (Path.getparent ()); //d. Get file all names on its path for (int i = 0; i < path.getnamecount () ; i++) { system.out.println ("name element " + i + " is: " + path.getname (i)) ; } // E. Obtaining Subpath system.out.println ("subpath (0,1): " + path.subpath (0, 1)); /** * 8. Get the file path uri for the browser to access */ system.out.println (Path.toUri ()); /** * 9. Converting a relative path to an absolute path */ path = paths.get ("C:" , "my" , "Test" , "1.log") ; system.out.println ( Path.toabsolutepath ()); try { path.torealpath ( Linkoption.nofollow_links); &nBsp; system.out.println (PATH); } catch (ioexception e) { e.printstacktrace (); } /** * 10. Converting path to file */ file file = path.tofile () ; system.out.println (file); /** * 11. Comparison of two paths */ path pAth01 = paths.get ("BNP.txt"); path path02 = paths.get ("AEGON.txt"); path path02_to_path01 = path02.relativize (PATH01); system.out.println (PATH02_TO_PATH01); }}
Pro Java7 nio.2 Learning (chapter I)