File是個(gè)文件類,可以用其增加、刪除、查找某種類型的文件或者文件夾,同時(shí)根據(jù)其成員變量的特點(diǎn)可以綜合利用,避免出現(xiàn)跨系統(tǒng)的時(shí)候出現(xiàn)錯(cuò)誤,并且查找時(shí)最好輸入絕對(duì)路徑,以免出現(xiàn)不存在的文件。使用遞歸時(shí)一定要主要好停止,以免棧內(nèi)存溢出。(推薦:java視頻教程)
一、簡(jiǎn)述IO操作:
當(dāng)需要把內(nèi)存中的數(shù)據(jù)存儲(chǔ)到持久化設(shè)備上的這個(gè)動(dòng)作稱為輸出(寫)output操作;
當(dāng)把持久設(shè)備上的數(shù)據(jù)讀取到內(nèi)存中的這個(gè)動(dòng)作稱為輸入(讀)input操作。
這個(gè)輸入和輸出的動(dòng)作稱為IO操作。
二、File類:
1、文件類:
成員變量:
import java.io.File; public class Demo01 { public static void main(String[] args) { //File類 //文件:File //目錄(文件夾):directory //路徑:path //路徑分隔符(與系統(tǒng)有關(guān)的)<windows里面是 ; linux里面是 : > System.out.println(File.pathSeparator); // ; //與系統(tǒng)有關(guān)的路徑名稱分隔符<windows里面是 \ linux里面是/ > System.out.println(File.separator); // \ } }
2、構(gòu)造函數(shù):
import java.io.File; public class Demo02 { public static void main(String[] args) { File file=new File("D:\\java\\b.txt"); //雙\\是轉(zhuǎn)義 System.out.println(file); File file2=new File("D:\\java","a.txt");//父路徑、子路徑--可以適用于多個(gè)文件的! System.out.println(file2); File parent=new File("D:\\java"); File file3=new File(parent,"a.txt");//File類的父路徑、子路徑 System.out.println(file3); } }
3、File類的獲取及文件的創(chuàng)建和刪除、判斷:
import java.io.File; import java.io.IOException; public class Demo03 { public static void main(String[] args) throws IOException { method06(); } public static void method01(){ File file=new File("D:\\java\\a.txt"); //獲取文件對(duì)象的絕對(duì)路徑 System.out.println(file.getAbsolutePath()); File file0=new File("src");//寫相對(duì)路徑的話,會(huì)自動(dòng)轉(zhuǎn)成絕對(duì)路徑,但是不去檢驗(yàn)文件是否真實(shí)存在(只會(huì)給翻譯回來(lái),可能根本不存在) D:\JAVA0322\Day16\src //獲取文件對(duì)象的絕對(duì)路徑 System.out.println(file0.getAbsolutePath()); File file00=new File("aa");//這個(gè)根本不存在 D:\JAVA0322\Day16\aa //獲取文件對(duì)象的絕對(duì)路徑 System.out.println(file00.getAbsolutePath()); //獲取文件對(duì)象的文件名或者目錄名 System.out.println(file.getName()); //獲取文件對(duì)象的路徑所對(duì)應(yīng)的字符串 類似于toString()方法 System.out.println(file.getPath()); //獲取文件的大?。ㄗ止?jié)---Long類型) System.out.println(file.length()); } //文件創(chuàng)建和刪除 public static void method02() throws IOException{ File file=new File("D:\\java\\d"); //創(chuàng)建文件 boolean flag=file.createNewFile();//都是創(chuàng)建的文件(最好都是加上后綴的),不能是文件夾 System.out.println(flag); } //文件刪除 public static void method03(){ File file=new File("D:\\java\\d"); //刪除文件(找不回來(lái)了) boolean flag=file.delete(); System.out.println(flag); } //文件判斷 public static void method04(){ File file=new File("D:\\java\\a.txt"); //判斷該文件對(duì)象所對(duì)應(yīng)的文件是否存在 System.out.println(file.exists()); //判斷該文件對(duì)象是否是文件夾 System.out.println(file.isDirectory()); //判斷該文件對(duì)象是否是文件 System.out.println(file.isFile()); } //創(chuàng)建文件夾 public static void method05(){ File file=new File("D:\\java\\d.txt");//windows系統(tǒng)內(nèi)文件夾名字不區(qū)分大小寫,最后這個(gè)是文件夾的名字 boolean flag=file.mkdir(); System.out.println(flag); } //創(chuàng)建文件夾 public static void method06(){ File file=new File("D:\\java\\d\\a\\b");//mkdirs()用于創(chuàng)建多級(jí)目錄,經(jīng)常用的方法,不加s不能創(chuàng)建多級(jí)目錄 boolean flag=file.mkdirs(); System.out.println(flag); } }
更多java知識(shí)請(qǐng)關(guān)注java基礎(chǔ)教程欄目。
? ??? ???? ???? ??????? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

JDBC ????? ???? ????? ?? ?? ?? ??? ?? ?? ??? ?? ? ?? ??? ?? ?? ?? ??? ???????. 1. ????? ????? Conn.SetAutoCommit (False)?? ??????. 2. ??? ? ????? ?? ?? SQL ??? ?????. 3. ?? ??? ??? ?? Conn.commit ()?? ???? ??? ???? ???? ?? ??? ???? Conn.Rollback ()?? ??????. ???, ? ??? ???? ????, ??? ???? ????, ?? ??? ??? ?? ??? ??? ???? ? ???????. ?? ?? ?? ???? ????? ??? ???? ?? ?? ???? ???? ??? ????? ?? ??? ??? ? ?? ???? ?? ????.

thejvmenablesjava? "WriteOnce, Runynywhere"??? ?? excodecodethroughfourmaincomponents : 1. theclassloadersubsystemloads, ??, ? intinitializes.classfilesusingbootsprap, extension, andapplicationclassloaders, ensuringsecureandlazyclasloa

?? ?? ? ?? ???? ???? ?? Java.Time ???? ???? ??????. 2. LocalDate, LocalDateTime ? LocalTime? ?? ?? ??? ??? ?????. 3. () ???? ???? ?? ??? ??? ????. 4. ???/???? ??? ???? ??? ????? ??? ??????. 5. ZonedDateTime ? Zoneid? ???? ???? ??????. 6. DateTimeFormatter? ?? ?? ? ?? ?? ?? ???; 7. ??? ?? ?? ?? ??? ????? ?? ??????. ?? Java? ?? ??? ???? ??? ??? ???? Java.Timeapi ??? ?? ??? ???????.

Pre-FormancetArtUptimeMoryUsage, Quarkusandmicronautleadduetocompile-timeprocessingandgraalvsupport, withquarkusoftenperforminglightbetterine serverless sinarios.2.thyvelopecosyste,

NetworkPortSandfirewallsworkTogetToenableCommunication whileensuringsecurity.1.networkportSarevirtualendpointsnumbered0–65535, Withwell-nownports like80 (http), 443 (https), 22 (ssh) ? 25 (smtp) ?? (specservices

Java 's Garbage Collection (GC)? ???? ???? ???? ??????, ?? ? ??? ??? ? ??? ??? ??? ??? ????. 1.GC? ?? ?? (? : ?? ??, ?? ???, ?? ?? ?)?? ??? ???? ????, ?? ? ??? ??? ???? ?????. 2. ?? ???? ????? ????, ?? ?? ??? ??? ???? ?? ??? ??????. 3. ?? ?? ?? ?? : ??? ?? (Eden, S0, S1)? ?? ????? ?????. ??? ??? ?? ? MajorGC? ???? ? ??? ? ????. Metaspace? ??? ?? ???? ?????. 4. JVM? ??? GC ??? ?????. SerialGC? ??? ?? ????? ?????. ParallelGC? ???? ??????. CMS? ?? ???

GradleisBetTerChoiceFormostNewProjectSduetoitssuperiorflexible, Performance, and ModernToolingsupport.1.Gradle'Sgroovy/kotlindslismoreConcisENDEXPRESSIVETHANMAVEN'SVOSEXML.2.GradleOutsMaveninbuildweedweedweedweedweedweedweedweedweedweedweedweedweedweede

DEFER? ??? ???? ?? ??? ??? ???? ? ?????. ?? ??? ?? ? ? ?? ????, ??? ??? ? ?? ?? (LIFO)? ??? ?????. 1. ?? ??? ??? ? ??? ?????. 2. ?? ??? ?? ??? ??? ????? ?????. 3. ?? ? ?? ?? ??? ? ????. 4. ??? ?????? ??? ??? ???? ?????. 5. ?? ??? ???? ?? ??? ?? ??? ?????. ??? ??? ?? ?? ? ???? ???? ? ????.
