Java IO流對文件File操作
什么是文件
文件,對我們并不陌生,文件是保存數(shù)據(jù)的地方,比如大家經(jīng)常使用的word文檔,txt文件,exce|文件..都是文件。它既可以保存一-張圖片也可以保持視頻,聲音...
文件流
文件在程序中是以流的形式來操作的。
流:數(shù)據(jù)在數(shù)據(jù)源(文件)和程序(內(nèi)存)之間經(jīng)歷的路徑。
輸入流:數(shù)據(jù)從數(shù)據(jù)源(文件)到程序(內(nèi)存)的路徑。
輸出流:數(shù)據(jù)從程序(內(nèi)存)到數(shù)據(jù)源(文件)的路徑。
可以把上面的流比作人喝水
從杯子里的水喝進(jìn)去人的胃里,就是輸入流,人把水吐出到杯子里就是輸出流。杯子就是磁盤,水就是文件。人就是Java程序,胃就是內(nèi)存。
常用的文件操作
創(chuàng)建文件
new File(String pathname) //根據(jù)路徑構(gòu)建一一個File對象
new File(File parent,String child) //根據(jù)父目錄文件+子路徑構(gòu)建
File(String parent,String child) //根據(jù)父目錄+子路徑構(gòu)建
createNewFile創(chuàng)建新文件
直接看代碼:
package com.io.file; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.Test; import java.io.File; import java.io.IOException; /** * @author 華子 * 文件的創(chuàng)建 */ public class FileCreate { public static void main(String[] args) { } // 第一種方法 new File(String path) @Test public void create01(){ String filePath = "E:\\news1.txt"; File file = new File(filePath); try { file.createNewFile(); System.out.println("文件創(chuàng)建成功"); } catch (IOException e) { System.out.println("文件創(chuàng)建失敗"); } } // 第二種方法 newFile(File parent,String path);父目錄+子路徑構(gòu)建 @Test public void create02(){ File parentFile = new File("e:\\"); String fileName = "news2.txt"; // 這里的file對象,只是一個Java對象,保存在內(nèi)存中 File file = new File(parentFile, fileName); // 只有createNewFile()了,才會出現(xiàn)在磁盤中 try { file.createNewFile(); System.out.println("文件創(chuàng)建成功"); } catch (IOException e) { System.out.println("文件創(chuàng)建失敗"); } } //方式3 new File(String parent, String child) //根據(jù)父日錄+子路徑構(gòu)建 @Test public void create03(){ String parentFile = "e:\\"; String childFile = "news\\news3.txt"; File file = new File(parentFile, childFile); try { file.createNewFile(); System.out.println("文件創(chuàng)建成功"); } catch (IOException e) { System.out.println("文件創(chuàng)建失敗"); } } }
運(yùn)行結(jié)果:
第一種方法就是在E盤中創(chuàng)建了一個news1.txt文件
第二種方法通過父目錄文件+子路徑構(gòu)建在E盤新建了一個 news2.txt 文件。
第三種就是 根據(jù)父目錄+子路徑構(gòu)建 在E盤中的news文件夾里創(chuàng)建了一個new3.txt文件
如圖:
獲取文件信息
getName、getAbsolutePath、 getParent、 length、 exists、 isFile、isDirectory
獲取上面創(chuàng)建的new3.txt文件。
直接看代碼:
package com.io.file; import org.junit.jupiter.api.Test; import java.io.File; /** * 獲取文件信息 */ public class FileInformation { public static void main(String[] args) { } // 獲取文件信息 @Test public void info(){ // 先創(chuàng)建文件對象 File file = new File("E:\\news\\news3.txt"); // 調(diào)用對應(yīng)的方法,得到對應(yīng)的信息 // getName、getAbsolutePath、getParent、Length、exists、isFile、isDirectory System.out.println("文件名稱:"+file.getName()); System.out.println("文件絕對路徑:"+file.getAbsolutePath()); System.out.println("文件父級目錄:"+file.getParent()); System.out.println("文件大?。?+file.length()); System.out.println("文件是否存在:"+file.exists());//true System.out.println("是不是一個文件:"+file.isFile());//true System.out.println("是不是一個目錄:"+file.isDirectory());//false } }
運(yùn)行結(jié)果:
目錄的操作和文件刪除
mkdir創(chuàng)建一級目錄、mkdirs創(chuàng)建多級目錄、delete刪除空目錄或文件
直接看案例:
package com.io.file; import org.junit.jupiter.api.Test; import java.io.File; /** * 目錄 */ public class Directory { public static void main(String[] args) { } // 判斷e:\news1.txt是否存在,如果存在就刪除 @Test public void m1(){ String filePath = "e:\\news1.txt"; File file = new File(filePath); if(file.exists()){ if(file.delete()){ System.out.println("文件已經(jīng)刪除...."); }else { System.out.println("文件刪除失敗..."); } }else { System.out.println("該文件不存在..."); } } //判斷D:\\demo02 這個目錄是否存在,存在就刪除,否則提示不存在 //這里我們需要體會到,在java編程中,目錄也被當(dāng)做文件 @Test public void m2(){ String filePath = "D:\\demo02"; File file = new File(filePath); if(file.exists()){ if(file.delete()){ System.out.println("目錄已經(jīng)刪除...."); }else { System.out.println("目錄刪除失敗..."); } }else { System.out.println("該目錄不存在..."); } } //判斷D:\\demo\\a\\b\\c 日錄是否存在,如果存在就提示經(jīng)存在,否則就創(chuàng)建 @Test public void m3(){ String directoryPath = "D:\\demo\\a\\b\\c"; File file = new File(directoryPath); if(file.exists()){ System.out.println("目錄已經(jīng)存在"); }else { if(file.mkdirs()){ System.out.println("創(chuàng)建成功"); }else { System.out.println("創(chuàng)建失敗"); } } } }
m1方法刪除news.txt文件,m2在D盤創(chuàng)建一級目錄demo,m3在D盤創(chuàng)建多級目錄
運(yùn)行結(jié)果:
到此這篇關(guān)于Java IO流對文件File操作的文章就介紹到這了,更多相關(guān)Java操作File內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot面試突擊之過濾器和攔截器區(qū)別詳解
過濾器(Filter)和攔截器(Interceptor)都是基于?AOP(Aspect?Oriented?Programming,面向切面編程)思想實現(xiàn)的,用來解決項目中某一類問題的兩種“工具”,但二者有著明顯的差距,接下來我們一起來看2022-10-10java:程序包org.apache.ibatis.annotations不存在報錯解決
這篇文章主要給大家介紹了關(guān)于java:程序包org.apache.ibatis.annotations不存在報錯的解決方法,這個錯誤是我在直接導(dǎo)入springboot項目的時候報錯的,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04PostgreSQL Docker部署+SpringBoot集成方式
本文介紹了如何在Docker中部署PostgreSQL和pgadmin,并通過SpringBoot集成PostgreSQL,主要步驟包括安裝PostgreSQL和pgadmin,配置防火墻,創(chuàng)建數(shù)據(jù)庫和表,以及在SpringBoot中配置數(shù)據(jù)源和實體類2024-12-12實例詳解Java實現(xiàn)圖片與base64字符串之間的轉(zhuǎn)換
這篇文章主要介紹了Java實現(xiàn)圖片與base64字符串之間的轉(zhuǎn)換實例代碼,非常不錯,具有參考借鑒價值,需要的朋友參考下2016-12-12詳解SpringMVC的url-pattern配置及原理剖析
這篇文章主要介紹了SpringMVC的url-pattern配置及原理剖析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06mybatis條件構(gòu)造器(EntityWrapper)的使用方式
這篇文章主要介紹了mybatis條件構(gòu)造器(EntityWrapper)的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03