13、javaにおけるいくつかの異なるファイルパスの違い(絶対パス、全パス)

2446 ワード


package com.tij.io.file;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 *   java              
 * @author guoyoujun
 * @date 2014-3-17
 */
public class JavaFilePath {

	/**
	 * java.io.File                   
	 * <p>getPath():                      ,               file  ,             
	 *     URL  ,    http          
	 * <p>getAbsolutePath():             ,                     ,          ,         
	 *  unix   ,                    , window                  
	 *<p>getCanonicalPath():                  ,                  ,          (window,unix)     
	 * @param args
	 * @throws IOException 
	 * @throws URISyntaxException 
	 */
	public static void main(String[] args) throws IOException, URISyntaxException {
		File file = new File("/Users/GYJ/java1.txt");
		printPaths(file);
		//relative path(    )
        file = new File("NewDB.properties");
        printPaths(file);
        //complex relative paths(        )
        file =new File("/Users/../GYJ/funshion/bbinfo.txt");
        printPaths(file);
        //URI paths(URL)
        file =new File(new URI("file:///Users/GYJ/java1.txt"));
        printPaths(file);

	}
	
	private static void printPaths(File f) throws IOException {
		System.out.println("AbsolutePath = " + f.getAbsolutePath());
		System.out.println("CanonicalPath = " + f.getCanonicalPath());
		System.out.println("Path = " + f.getPath());
	} 

}
out put============
AbsolutePath = C:\Users\GYJ\java1.txt
CanonicalPath = C:\Users\GYJ\java1.txt
Path = \Users\GYJ\java1.txt


AbsolutePath = C:\Users\GYJ\workspace\java_workspace\io\NewDB.properties
CanonicalPath = C:\Users\GYJ\workspace\java_workspace\io\NewDB.properties
Path = NewDB.properties


AbsolutePath = C:\Users\..\GYJ\funshion\bbinfo.txt
CanonicalPath = C:\GYJ\funshion\bbinfo.txt
Path = \Users\..\GYJ\funshion\bbinfo.txt


AbsolutePath = C:\Users\GYJ\java1.txt
CanonicalPath = C:\Users\GYJ\java1.txt
Path = \Users\GYJ\java1.txt