Java文件與IO流操作原理詳細分析
一、文件
1、基本解釋
(1)什么是文件?
文件是保存數(shù)據(jù)的地方,比如大家經(jīng)常使用的word文檔、txt文件、excel文件等都是文件。它還可以保存一張圖片,也可以保存視頻聲音
(2)文件流
流 :數(shù)據(jù)在數(shù)據(jù)源(文件)和程序(內(nèi)存)之間經(jīng)歷的路徑
輸入流 : 數(shù)據(jù)從數(shù)據(jù)源(文件)到程序(內(nèi)存)的路徑
輸出流 :數(shù)據(jù)從程序(內(nèi)存)到數(shù)據(jù)源(文件)的路徑
2、常用的文件操作
(1)相關(guān)方法
- new File(String pathname) --> 根據(jù)路徑構(gòu)建一個File對象
- new File(File parent,String child) --> 根據(jù)父目錄文件+子路徑構(gòu)建
- new File(String parent,String child) --> 根據(jù)父目錄+子路徑構(gòu)建
- createNewFile 創(chuàng)建新文件
(2)代碼示例
(①)方式一:new File(String pathname)
//方式1 new File(String pathname) public void create01(){ //定好路徑 String filePath = "D:\\news1.txt"; //創(chuàng)建路徑對象 File file = new File(filePath); try { //根據(jù)路徑創(chuàng)建文件 file.createNewFile(); System.out.println("創(chuàng)建文件成功"); } catch (IOException e) { e.printStackTrace(); } }
(②)方式二:new File(File parent,String child)
//第二種方式 new File(File parent,String child) ,根據(jù)父目錄文件 + 子路徑構(gòu)建 //D:\\news2.txt public void create02(){ File parentFile = new File("D:\\"); String fileName = "news2.txt"; //創(chuàng)建路徑對象 File file = new File(parentFile, fileName); try { //根據(jù)路徑創(chuàng)建文件 file.createNewFile(); System.out.println("創(chuàng)建成功~"); } catch (IOException e) { e.printStackTrace(); } }
(③)方式三:new File(String parent,String child)
//第三種方式 new File(String parent,String child) ,根據(jù)父目錄文件 + 子路徑構(gòu)建 //D:\\news3.txt public void create03(){ String parentFile = "D:\\"; String fileName = "news3.txt"; File file = new File(parentFile, fileName); try { file.createNewFile(); System.out.println("創(chuàng)建成功~"); } catch (IOException e) { e.printStackTrace(); } }
3、獲取文件相關(guān)信息
(1)常用方法:代碼示例
//獲取文件的信息 public void info(){ //先創(chuàng)建文件對象 File file = new File("D:\\news1.txt"); //調(diào)用相應(yīng)的方法,得到對應(yīng)的信息 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()); }
4、目錄操作和文件刪除
- mkdir 創(chuàng)建一級目錄
- mkdirs 創(chuàng)建多級目錄
- delete 刪除空目錄或文件
==》代碼示例1:
//判斷 d:\\news1.txt 是否存在,如果存在就刪除 public void m1(){ //文件路徑 String filePath = "d:\\news1.txt"; //創(chuàng)建文件路徑對象 File file = new File(filePath); //首先判斷文件是否存在 if (file.exists()){ //如果存在,就刪除 if (file.delete()){ System.out.println(filePath + "刪除成功"); }else { System.out.println("刪除失敗"); } }else { System.out.println("該文件不存在....."); } }
==》代碼示例2:
//判斷 d:\\demo02 是否存在,如果存在就刪除 //這里我們需要體會到,在Java編程中,目錄也被當做文件 public void m2(){ //文件路徑 String filePath = "d:\\demo02"; //創(chuàng)建文件路徑對象 File file = new File(filePath); //首先判斷文件是否存在 if (file.exists()){ //如果存在,就刪除 if (file.delete()){ System.out.println(filePath + "刪除成功"); }else { System.out.println("刪除失敗"); } }else { System.out.println("該目錄不存在....."); } }
==》代碼示例3:
//判斷:D:\\demo\\a\\b\\c 目錄是否存在,如果存在就提示已經(jīng)存在,否則就創(chuàng)建 public void m3(){ //文件路徑 String directoryPath = "D:\\demo\\a\\b\\c"; //路徑對象 File file = new File(directoryPath); //首先判斷文件是否存在 if (file.exists()){ System.out.println(directoryPath + "存在"); }else { if (file.mkdirs()){ System.out.println(directoryPath + "創(chuàng)建成功"); }else { System.out.println("創(chuàng)建失敗"); } } }
二、IO流原理及分類
1、IO流原理
- I / O是 Input / Output的縮寫,I/O技術(shù)是非常實用的技術(shù),用于處理數(shù)據(jù)傳輸,如讀文件、寫文件,網(wǎng)結(jié)通訊等
- Java程序中,對于數(shù)據(jù)的輸入/輸出操作以”流(stream)" 的方式進行。
- java.io 包下提供了各種“流”的類和接口,用以獲取不同種類的數(shù)據(jù),并通過方法輸入或輸出數(shù)據(jù)
- 輸入(input) : 讀取外部數(shù)據(jù)(磁盤、光盤等存儲設(shè)備的數(shù)據(jù))到程序(內(nèi)存)中。
- 輸出(output): 將程序(內(nèi)存)數(shù)據(jù)輸出到磁盤、 光盤等存儲設(shè)備中
- 原理示意圖:
2、流的分類
- 按操作數(shù)據(jù)單位不同分為 : 字節(jié)流(8 bit)二進制文件,字符流(按字符)文本文件
- 按數(shù)據(jù)流的流向不同分為 : 輸入流,輸出流
- 按流的角色的不同分為 : 節(jié)點流,處理流/包裝流
注意:
1、Java的I0流共涉及40多個類 實際上非常規(guī)則 都是從如上4個抽象基類派生的
2、由這四個類派生出來的子類名稱都是以其父類名作為子類名后綴
3、IO流體系圖
到此這篇關(guān)于Java文件與IO流操作原理詳細分析的文章就介紹到這了,更多相關(guān)Java文件與IO流內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
劍指Offer之Java算法習(xí)題精講二叉樹的構(gòu)造和遍歷
跟著思路走,之后從簡單題入手,反復(fù)去看,做過之后可能會忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會發(fā)現(xiàn)質(zhì)的變化2022-03-03基于<aop:aspect>與<aop:advisor>的區(qū)別
這篇文章主要介紹了<aop:aspect>與<aop:advisor>的區(qū)別,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11基礎(chǔ)不牢,地動山搖,Java基礎(chǔ)速來刷刷
基礎(chǔ)不牢,地動山搖,快來一起學(xué)習(xí)一下基礎(chǔ)吧,不斷地學(xué)習(xí)就算是基礎(chǔ)也會有新的認知和收獲,加油2021-08-08struts2+jsp+jquery+Jcrop實現(xiàn)圖片裁剪并上傳實例
本篇文章主要介紹了struts2+jsp+jquery+Jcrop實現(xiàn)圖片裁剪并上傳實例,具有一定的參考價值,有興趣的可以了解一下。2017-01-01