深入了解Java?File對(duì)象的使用
1.File對(duì)象
java封裝的一個(gè)操作文件及文件夾(目錄)的對(duì)象??梢圆僮鞔疟P上的任何一個(gè)文件和文件夾。
2.創(chuàng)建文件
方式一:根據(jù)路徑構(gòu)建一個(gè)File對(duì)象new File(path)
//方式一
@Test
public void create01(){
try {
String path = URLDecoder.decode("D:\\博客園\\wjj1.txt","UTF-8");//解決中文亂碼,轉(zhuǎn)UTF-8
File file = new File(path);
file.createNewFile();
System.out.println("創(chuàng)建成功01");
} catch (UnsupportedEncodingException e) {//decode方法需要拋異常或捕獲異常
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}方式二:根據(jù)父目錄文件和子目錄路徑構(gòu)建一個(gè)File對(duì)象new File(File,Spath)
//方式二
@Test
public void create02(){
String path = null;
try {
path = URLDecoder.decode("D:\\博客園","UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
File parentFile = new File(path);//父目錄文件
String fileName = "wjj2.txt";//子路徑
File file = new File(parentFile, fileName);
try {
file.createNewFile();
System.out.println("創(chuàng)建成功02");
} catch (IOException e) {
e.printStackTrace();
}
}方式三:根據(jù)父目錄路徑和子目錄路徑構(gòu)建一個(gè)File對(duì)象new File(Fpath,Spath)
//方式三
@Test
public void create03() throws Exception{//拋異常
String path = URLDecoder.decode("D:\\博客園","UTF-8");
String filePath = "wjj3.txt";
File file = new File(path, filePath);
file.createNewFile();
System.out.println("創(chuàng)建成功03");
}運(yùn)行結(jié)果:

3.文件的相關(guān)操作
文件的路徑相關(guān)和判斷功能的構(gòu)造方法
@Test
public void info() throws Exception{
//創(chuàng)建文件對(duì)象
String path = URLDecoder.decode("D:\\博客園\\wjj1.txt","UTF-8");
File file = new File(path);
System.out.println("文件名:"+file.getName());
System.out.println("文件絕對(duì)路徑:"+file.getAbsolutePath());
System.out.println("文件父目錄:"+file.getParent());
System.out.println("文件大小(字節(jié)):"+file.length());
System.out.println("文件是否存在:"+file.exists());
System.out.println("是否是文件:"+file.isFile());
System.out.println("是否是目錄:"+file.isDirectory());
}UTF-8一個(gè)英文一個(gè)字節(jié),一個(gè)漢字三個(gè)字節(jié)
運(yùn)行結(jié)果:

文件刪除操作的構(gòu)造方法
@Test
public void fileDelete() throws Exception{
String path = URLDecoder.decode("D:\\博客園\\wjj1.txt","UTF-8");
File file = new File(path);
if (file.exists()){
if (file.delete()){
System.out.println(path+"刪除成功");
}else {
System.out.println(path+"刪除失敗");
}
}else {
System.out.println("文件不存在");
}
}文件創(chuàng)建目錄操作的構(gòu)造方法
@Test
public void isMkdir() throws Exception{
String path = URLDecoder.decode("D:\\博客園\\wjj1","UTF-8");
File file = new File(path);
if (file.exists()){
System.out.println(path+"該目錄已存在");
}else {
if (file.mkdirs()){
System.out.println("創(chuàng)建成功");
}else {
System.out.println("創(chuàng)建失敗");
}
}
}運(yùn)行結(jié)果:

到此這篇關(guān)于深入了解Java File對(duì)象的使用的文章就介紹到這了,更多相關(guān)Java File對(duì)象內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
探究springboot中的TomcatMetricsBinder
springboot的TomcatMetricsBinder主要是接收ApplicationStartedEvent然后創(chuàng)建TomcatMetrics執(zhí)行bindTo進(jìn)行注冊(cè),TomcatMetrics主要注冊(cè)了globalRequest、servlet、cache、threadPool、session相關(guān)的指標(biāo),本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2023-11-11
Java實(shí)現(xiàn)的文件上傳下載工具類完整實(shí)例【上傳文件自動(dòng)命名】
這篇文章主要介紹了Java實(shí)現(xiàn)的文件上傳下載工具類,結(jié)合完整實(shí)例形式分析了java針對(duì)文件上傳下載操作的相關(guān)實(shí)現(xiàn)技巧,并且針對(duì)上傳文件提供了自動(dòng)命名功能以避免文件命名重復(fù),需要的朋友可以參考下2017-11-11
Springboot注入成員變量HttpServletRequest的原理分析
這篇文章主要介紹了Springboot注入成員變量HttpServletRequest的原理分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
Spring?Data?JPA?在?@Query?中使用投影的方法示例詳解
這篇文章主要介紹了Spring?Data?JPA?在?@Query?中使用投影的方法,大家需要注意如果要在 @Query 中使用投影,必須要主動(dòng)聲明要查詢的字段,并且主動(dòng)寫明字段的別名才行,本文通過sql代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-07-07
SpringMVC獲取請(qǐng)求參數(shù)實(shí)現(xiàn)方法介紹
Spring MVC 是 Spring 提供的一個(gè)基于 MVC 設(shè)計(jì)模式的輕量級(jí) Web 開發(fā)框架,本質(zhì)上相當(dāng)于 Servlet,Spring MVC 角色劃分清晰,分工明細(xì),這篇文章主要介紹了SpringMVC實(shí)現(xiàn)獲取請(qǐng)求參數(shù)方法2022-11-11
Sleuth(Micrometer)+ZipKin分布式鏈路問題小結(jié)
在微服務(wù)架構(gòu)中,分布式鏈路追蹤技術(shù)成為了解決系統(tǒng)復(fù)雜調(diào)用問題的關(guān)鍵,本文介紹了其他鏈路追蹤方案,如Cat、Pinpoint和Skywalking,展示了分布式鏈路追蹤技術(shù)的多樣化,感興趣的朋友一起看看吧2024-10-10
SpringCloud Feign遠(yuǎn)程調(diào)用與自定義配置詳解
Feign是Netflix公司開發(fā)的一個(gè)聲明式的REST調(diào)用客戶端; Ribbon負(fù)載均衡、 Hystrⅸ服務(wù)熔斷是我們Spring Cloud中進(jìn)行微服務(wù)開發(fā)非常基礎(chǔ)的組件,在使用的過程中我們也發(fā)現(xiàn)它們一般都是同時(shí)出現(xiàn)的,而且配置也都非常相似2022-11-11

