「Java入門」FileクラスのgetPath、getAbsolutePath、getCanonicalPathの使い方

Javaコード:
package javaexc;
import java.io.File;
import java.io.IOException;

public class KdfFilepath {

public static void main(String[] args) {

try {
System.out.println(“—–デフォルトの相対パス1—–“);
File file1 =new File(“..\\src\\democft.txt");
System.out.println(file1.getPath());
System.out.println(file1.getAbsolutePath());
System.out.println(file1.getCanonicalPath());
System.out.println(“—–デフォルトの相対パス2——“);
File file =new File(“.\\democft.txtt");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
System.out.println(file.getCanonicalPath());

System.out.println(“—–デフォルトの絶対パス——“);
File file2 =new File(“C:\\bak\\test\\democft.txt");
System.out.println(file2.getPath());
System.out.println(file2.getAbsolutePath());
System.out.println(file2.getCanonicalPath());
} catch (IOException e) {
// TODOAuto-generated catch block
e.printStackTrace();
}
}

}

実行結果
—–デフォルトの相対パス1——
..\src\democft.txt
C:\bak\javaexc\..\src\democft.txt
C:\bak\src\democft.txt
—–デフォルトの相対パス2——
.\democft.txtt
C:\bak\javaexc\.\democft.txtt
C:\bak\javaexc\democft.txtt
—–デフォルトの絶対パス——
C:\bak\test\democft.txt
C:\bak\test\democft.txt
C:\bak\test\democft.txt

Java

Posted by arkgame