File 類? 遞歸  java.io.File  類 是文件和目錄路徑名 的抽象表示,主要用于文件和目錄 的創建、查找和刪除等操作 。 
 java.io.File類  文件和文件夾(目錄) 封裝為了一個File類 ,我們可以使用File類對文件和文件夾 進行操作創建一個文件/文件夾 ? File類是一個與系統無關的類,任何的操作系統都可以使用這個類中的方法 
 ? ? 重點:記住這三個單詞文件夾/目錄 
 
 
 ? ? ? ? /*pathSeparator  與系統有關的路徑分隔符 ,為了方便,它被表示為一個字符串。pathSeparatorChar  與系統有關的路徑分隔符。
 ? ? ? ? ? ? static String separator  與系統有關的默認名稱分隔符 ,為了方便,它被表示為一個字符串。separatorChar  與系統有關的默認名稱分隔符。
 ? ? ? ? ? ? 操作路徑:路徑不能寫死了
 
 
package com.itheima.demo01.File;import java.io.File;public class Demo01File {public static void main(String[] args) {String pathSeparator = File.pathSeparator;System.out.println(pathSeparator);//路徑分隔符 windows:分號;  linux:冒號:String separator = File.separator;System.out.println(separator);// 文件名稱分隔符 windows:反斜杠\  linux:正斜杠/}} 
? ? 路徑: 絕對路徑:是一個完整的路徑 盤符(c:,D:)開始的路徑  相對路徑:是一個簡化的路徑 當前項目的根目錄 (C:\\Users\itcast\\IdeaProjects\\shungyuan)C:\\Users\itcast\\IdeaProjects\\shungyuan\\123.txt? ->簡化為: 123.txt(可以省略項目的根目錄) 路徑是不區分大小寫 路徑中的文件名稱分隔符windows使用反斜杠,反斜杠是轉義字符,兩個反斜杠代表一個普通的反斜杠 
 
 
 ? ? ? ? File(String pathname)  通過將給定路徑名字符串 轉換為抽象路徑名來創建一個新 File 實例 。路徑 可以是以文件結尾 ,也可以是以文件夾結尾 路徑可以是存在,也可以是不存在 創建File對象,只是把字符串路徑封裝為File對象,不考慮路徑的真假情況 
 
 
private static void show01() {File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\a.txt");System.out.println(f1);//重寫了Object類的toString方法 C:\Users\itcast\IdeaProjects\shungyuan\a.txtFile f2 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan");System.out.println(f2);//C:\Users\itcast\IdeaProjects\shungyuanFile f3 = new File("b.txt");System.out.println(f3);//b.txt} 
 
? ? ? ? File(String parent, String child) 根據 parent 路徑名字符串和 child 路徑名字符串創建一個新 File 實例。String parent:父路徑 
 ?File(File parent, String child) 根據 parent 抽象路徑名和 child 路徑名字符串創建一個新 File 實例。 File parent:父路徑 
 
 
package com.itheima.demo01.File;import java.io.File;public class Demo02File {public static void main(String[] args) {/*File類的構造方法*///show02("c:\\","a.txt");//c:\a.txt//show02("d:\\","a.txt");//d:\a.txtshow03();File f = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan");long length = f.length();System.out.println(length);}private static void show03() {File parent = new File("c:\\");File file = new File(parent,"hello.java");System.out.println(file);//c:\hello.java}private static void show02(String parent, String child) {File file = new File(parent,child);System.out.println(file);//c:\a.txt}}
 
 
 
 
 
常用方法  
 
 
 ?public String getAbsolutePath()  :返回此File的絕對路徑名字符串。
  private static void show01() {File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\a.txt");String absolutePath1 = f1.getAbsolutePath();System.out.println(absolutePath1);//C:\Users\itcast\IdeaProjects\shungyuan\a.txtFile f2 = new File("a.txt");String absolutePath2 = f2.getAbsolutePath();System.out.println(absolutePath2);//C:\Users\itcast\IdeaProjects\shungyuan\a.txt} 
?
 
 
 ? ? ? ? public String getPath()  :將此File轉換為路徑名字符串 
 ? ? ? ? toString方法調用的就是getPath方法 toString()  {getPath(); 
 private static void show02() {File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\a.txt");File f2 = new File("a.txt");String path1 = f1.getPath();System.out.println(path1);//C:\Users\itcast\IdeaProjects\shungyuan\a.txtString path2 = f2.getPath();System.out.println(path2);//a.txtSystem.out.println(f1);//C:\Users\itcast\IdeaProjects\shungyuan\a.txtSystem.out.println(f1.toString());//C:\Users\itcast\IdeaProjects\shungyuan\a.txt} 
?
 
 
public String getName() ?:返回由此File表示的文件或目錄的名稱。結尾部分(文件/文件夾) 
   private static void show03() {File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\a.txt");String name1 = f1.getName();System.out.println(name1);//a.txtFile f2 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan");String name2 = f2.getName();System.out.println(name2);//shungyuan} 
?
 
 
?public long length()  ?:返回由此File表示的文件的長度 文件的大小,以字節為單位 文件夾是沒有大小概念的,不能獲取文件夾的大小 
  private static void show04() {File f1 = new File("C:\\develop\\a\\1.jpg");long l1 = f1.length();System.out.println(l1);//780831字節File f2 = new File("C:\\develop\\a\\2.jpg");System.out.println(f2.length());//0File f3 = new File("C:\\develop\\a");System.out.println(f3.length());//0 文件夾沒有大小概念的} 
?
 
 
 
 
 
 
?public boolean exists()  :此File表示的文件或目錄是否實際存在。
 private static void show01() {File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan");System.out.println(f1.exists());//trueFile f2 = new File("C:\\Users\\itcast\\IdeaProjects\\shung");System.out.println(f2.exists());//falseFile f3 = new File("shungyuan.iml");//相對路徑 C:\Users\itcast\IdeaProjects\shungyuan\shungyuan.imlSystem.out.println(f3.exists());//trueFile f4 = new File("a.txt");System.out.println(f4.exists());//false} 
?
 
 
?public boolean isDirectory()  :此File表示的是否為目錄。文件夾結尾 isFile()  :此File表示的是否為文件。電腦的硬盤中只有文件/文件夾,兩個方法是互斥 ? 這兩個方法使用前提,路徑必須是存在的,否則都返回false 
 private static void show02() {File f1 = new File("C:\\Users\\itcast\\IdeaProjects\\shung");//不存在,就沒有必要獲取if(f1.exists()){System.out.println(f1.isDirectory());System.out.println(f1.isFile());}File f2 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan");if(f2.exists()){System.out.println(f2.isDirectory());//trueSystem.out.println(f2.isFile());//false}File f3 = new File("C:\\Users\\itcast\\IdeaProjects\\shungyuan\\shungyuan.iml");if(f3.exists()){System.out.println(f3.isDirectory());//falseSystem.out.println(f3.isFile());//true}} 
?
                            總結 
                            
                                以上是生活随笔 為你收集整理的File类的知识1 的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                            
                                如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。