深入了解Java?File對象的使用
1.File對象
java封裝的一個操作文件及文件夾(目錄)的對象。可以操作磁盤上的任何一個文件和文件夾。
2.創(chuàng)建文件
方式一:根據(jù)路徑構(gòu)建一個File對象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)建一個File對象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)建一個File對象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");
}運行結(jié)果:

3.文件的相關(guān)操作
文件的路徑相關(guān)和判斷功能的構(gòu)造方法
@Test
public void info() throws Exception{
//創(chuàng)建文件對象
String path = URLDecoder.decode("D:\\博客園\\wjj1.txt","UTF-8");
File file = new File(path);
System.out.println("文件名:"+file.getName());
System.out.println("文件絕對路徑:"+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一個英文一個字節(jié),一個漢字三個字節(jié)
運行結(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)建失敗");
}
}
}運行結(jié)果:

到此這篇關(guān)于深入了解Java File對象的使用的文章就介紹到這了,更多相關(guān)Java File對象內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決FeignClient發(fā)送post請求異常的問題
這篇文章主要介紹了FeignClient發(fā)送post請求異常的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
SpringMVC中Model和ModelAndView的EL表達式取值方法
下面小編就為大家分享一篇SpringMVC中Model和ModelAndView的EL表達式取值方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
Spring Boot容器加載時執(zhí)行特定操作(推薦)
這篇文章主要介紹了Spring Boot容器加載時執(zhí)行特定操作及spring內(nèi)置的事件,需要的朋友可以參考下2018-01-01
java中Calendar與Date類型互相轉(zhuǎn)換的方法
這篇文章主要介紹了java中Calendar與Date類型互相轉(zhuǎn)換的方法,Calendar與Date類型是我們?nèi)粘i_發(fā)中常用的兩種數(shù)據(jù)類型,它們用于不同的場景,兩者具有不同的方法,接下來通過實例給大家詳解,需要的朋友可以參考下2022-09-09
Spring JPA聯(lián)表查詢之OneToMany源碼解析
這篇文章主要為大家介紹了Spring JPA聯(lián)表查詢之OneToMany源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04

