File類的構造方法
阿新 • • 發佈:2020-11-15
package com.chunzhi.Test01.File; import java.io.File; /* File類的構造方法 */ public class Test02File { public static void main(String[] args) { method03(); } /* File(String pathname)通過將給定路徑名字串轉換為抽象路徑名來建立一個新File例項。 引數: String pathname:字串的路徑名稱 路徑可以是以檔案結尾或資料夾結尾 路徑可以是相對路徑或絕對路徑 路徑可以是存在或不存在 建立File物件,只是把字串路徑封裝為File物件,不考慮路徑的真假情況*/ public static void method01() { // 資料夾結尾 File f = new File("C:\\Other\\IdeaProjects"); System.out.println(f); // 檔案結尾 File f1 = new File("C:\\Other\\IdeaProjects\\a.txt"); System.out.println(f1); // 相對路徑 File f2 = new File("Test01File.java"); System.out.println(f2); }/* File(String parent, String child)根據parent路徑名字字串和child路徑名字字串建立了一個新File例項。 引數:把路徑分成了兩部分 String parent:父路徑 String child:子路徑 好處: 父路徑和子路徑,可以單獨書寫,使用起來靈活;父子路徑都可以變化 */ public static void method02(String parent, String child) { File f= new File(parent, child); System.out.println(f); } /* File(File parent, String chile)根據parent抽象路徑和child路徑名字字串建立一個新File例項。 引數:把路徑分成了兩部分 File parent:父路徑 String child:子路徑 好處: 父路徑和子路徑,可以單獨書寫,使用起來靈活;父路徑和子路徑都可以變化 父路徑是File型別,可以使用File的方法對路徑進行一些操作再使用路徑建立物件 */ public static void method03() { File parent = new File("c:\\"); File f = new File(parent, "Hello.java"); System.out.println(f); } }