Java細數(shù)IO流底層原理到方法使用
一、什么是IO流
輸入流和輸出流。
- 輸入流:數(shù)據(jù)從數(shù)據(jù)源(文件)到程序(內(nèi)存)的路徑
- 輸出流:數(shù)據(jù)從程序(內(nèi)存)到數(shù)據(jù)源(文件)的路徑
二、常用的文件操作
學習目標:創(chuàng)建文件對象相關構造器和方法
new File(String pathname)//根據(jù)路徑構建一個File對象
new File(File parent,String child)//根據(jù)父目錄文件+子路徑構建
new File(String parent,String child)//根據(jù)父目錄+子路徑構建
學習任務:在e盤下,創(chuàng)建文件news1.txt、news2.txt、news3.txt用三種不同方式創(chuàng)建。
三種方式簡單看一下就行,后面會經(jīng)常遇到。
new File(String pathname)//根據(jù)路徑構建一個File對象
package com.file; import java.io.*; public class FileCreate { public static void main(String[] args) { //方式 1 new File(String pathname) String filePath = "e:\\news1.txt"; File file = new File(filePath); try { //創(chuàng)建新文件 file.createNewFile(); System.out.println("文件創(chuàng)建成功"); } catch (IOException e) { e.printStackTrace(); } } }
new File(File parent,String child)//根據(jù)父目錄文件+子路徑構建
package com.file; import java.io.*; public class FileCreate { public static void main(String[] args) { //方式 2 new File(File parent,String child) //根據(jù)父目錄文件+子路徑構建 //e:\\news2.txt File parentFile = new File("e:\\"); String fileName = "news2.txt"; //這里的 file 對象,在 java 程序中,只是一個對象 //只有執(zhí)行了 createNewFile 方法,才會真正的,在磁盤創(chuàng)建該文件 File file = new File(parentFile, fileName); try { file.createNewFile(); System.out.println("創(chuàng)建成功~"); } catch (IOException e) { e.printStackTrace(); } } }
new File(String parent,String child)//根據(jù)父目錄+子路徑構建
package com.file; import java.io.*; public class FileCreate { public static void main(String[] args) { //方式 3 new File(String parent,String child) //根據(jù)父目錄+子路徑構建 String parentPath = "e:\\"; String fileName = "news3.txt"; File file = new File(parentPath, fileName); try { file.createNewFile(); System.out.println("創(chuàng)建成功~"); } catch (IOException e) { e.printStackTrace(); } } }
三、獲取文件的相關信息
getName、getAbsolutePath、getParent、length、exists、isFile、isDirectory
學習任務:獲取文件的大小、文件名、路徑、父File,是文件還是目錄(目錄本質(zhì)也是文件,一種特殊的文件),是否存在
四、目錄的操作和文件刪除
學習任務:
- 判斷e:\news1.txt是否存在,如果存在就刪除
- 判斷e:\\demo02是否存在,存在就刪除,否則提示不存在
- 判斷e:\\demo\a\b\c目錄是否存在,如果存在就提示已經(jīng)存在,否則就創(chuàng)建
package com.collection; import java.io.File; public class Delete { public static void main(String[] args) { String filePath="e:\\news1.txt"; File file=new File(filePath); if(file.exists()){ file.delete(); }else { System.out.println("否則文件不存在~"); } } }
package com.collection; import java.io.File; public class Delete { public static void main(String[] args) { String filePath="e:\\demo02"; File file=new File(filePath); if(file.exists()){ if(file.delete()){ System.out.println(filePath+"刪除成功"); } else{ System.out.println(filePath+"刪除失敗"); } }else { System.out.println("否則目錄不存在~"); } } }
package com.collection; import java.io.*; public class Delete { public static void main(String[] args) { String directoryPath="e:\\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(directoryPath+"創(chuàng)建失敗。。"); } } } }
按操作數(shù)據(jù)單位不同分為:字節(jié)流(8bit)二進制文件,字符流(按字符)文本文件
按數(shù)據(jù)流的流向不同分為:輸入流,輸出流
按流的角色的不同分為:節(jié)點流,處理流/包裝流
字節(jié)流:InputStream,OutputStream
字符流:Reader,Writer
五、IO流體系圖-常用的類
六、FileInputStream常用方法
學習任務:請使用 FileInputStream 讀取 hello.txt 文件,并將文件內(nèi)容顯示
先在e盤下創(chuàng)建hello.txt輸入內(nèi)容hello world
package com.FileInputStream; import java.io.FileInputStream; import java.io.IOException; //字節(jié)流文件的輸入程序 public class FileInputStream_ { public static void main(String[] args) { String filePath="e:\\hello.txt"; int readData=0; FileInputStream fileInputStream=null; try { //創(chuàng)建 FileInputStream 對象,用于讀取文件 fileInputStream=new FileInputStream(filePath); //從該輸入流讀取一個字節(jié)的數(shù)據(jù)。 如果沒有輸入可用,此方法將阻止。 //如果返回-1 , 表示讀取完畢 while ((readData = fileInputStream.read()) != -1) { System.out.print((char)readData);//轉(zhuǎn)成 char 顯示 } } catch (IOException e) { e.printStackTrace(); }finally { try { //關閉文件流 fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
七、FileOutputStream常用方法
學習任務:請使用 FileOutputStream 在 abc.txt 文件,中寫入 “hello,world”. 如果文件不存在,會創(chuàng)建 文件(注意:前提是目錄已經(jīng)存在.)
package com.hspedu.outputstream_; import java.io.*; public class FileOutputStream01 { public static void main(String[] args) { String filePath = "D:\\abc.txt"; FileOutputStream fileOutputStream = null; try { //得到 FileOutputStream 對象 對象 //1. new FileOutputStream(filePath) 創(chuàng)建方式,當寫入內(nèi)容是,會覆蓋原來的內(nèi)容 // 2. new FileOutputStream(filePath, true) 創(chuàng)建方式,當寫入內(nèi)容是,是追加到文件后面 fileOutputStream = new FileOutputStream(filePath); //寫入一個字節(jié) //fileOutputStream.write('H'); //寫入字符串 String str = "hello,world!"; //str.getBytes() 可以把 字符串-> 字節(jié)數(shù)組 //fileOutputStream.write(str.getBytes()); /* write(byte[] b, int off, int len) 將 len 字節(jié)從位于偏移量 off 的指定字節(jié)數(shù)組寫入此文件輸出流 */ fileOutputStream.write(str.getBytes(), 0, 3); } catch (IOException e) { e.printStackTrace(); } finally { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
學習任務:編程完成圖片/音樂 的拷貝
package com.hspedu.outputstream_; import java.io.*; public class FileCopy { public static void main(String[] args) { String srcFilePath="e:\\9030.jpg"; String destFilePath="e:\\9031.jpg"; FileInputStream fileInputStream=null; FileOutputStream fileOutputStream=null; try { fileInputStream=new FileInputStream(srcFilePath); fileOutputStream=new FileOutputStream(destFilePath); //定義一個字節(jié)數(shù)組,提高讀取效果 byte[] buf=new byte[1024]; int readLen=0; while ((readLen=fileInputStream.read(buf))!=-1){ fileOutputStream.write(buf,0,readLen); } System.out.println("拷貝成功!"); } catch (IOException e) { e.printStackTrace(); }finally { try{ if(fileInputStream!=null){ fileInputStream.close(); } if(fileOutputStream!=null){ fileInputStream.close(); } }catch (IOException e){ e.printStackTrace(); } } } }
八、FileReader常用方法
- new FileReader(File/String)
- read:每次讀取單個字符,返回該字符,如果到文件末尾返回-1
- read(char[]):批量讀取多個字符到數(shù)組,返回讀取到的字符數(shù),如果到文件末尾返回-1
- 相關API:
- new String(char[]):將char[]轉(zhuǎn)換成String
- new String(char[],off,len):將char[]的指定部分轉(zhuǎn)換成String
九、FileWriter常用方法
- new FileWriter(File/String):覆蓋模式,相當于流的指針在首端
- new FileWriter(File/String,true):追加模式,相當于流的指針在尾端
- write(int):寫入單個字符
- write(char[]):寫入指定數(shù)組
- write(char[],off,len):寫入指定數(shù)組的指定部分
- write(string):寫入單個字符
- write(string[],off,len):寫入字符串的指定部分
- 相關API:String類:toCharArray:將String轉(zhuǎn)換成char[]
- FileWriter使用后,必須要關閉(close)或刷新(flush), 否則寫入不到指定的文件!
學習任務:使用 FileReader 從 story.txt ,這一步先在story.txt存在數(shù)據(jù),然后在端口輸出數(shù)據(jù)顯示出來
package com.reader_; import java.io.FileReader; import java.io.IOException; public class ReadFile01 { public static void main(String[] args) { String filePath="e:\\story.txt"; FileReader fileReader=null; int data=0; //創(chuàng)建FileReader對象 try{ fileReader =new FileReader(filePath); //循環(huán)讀取 使用 read, 單個字符讀取 while((data=fileReader.read())!=-1){ //data數(shù)值為整數(shù)型,強制轉(zhuǎn)換為字符 System.out.print((char)data); } }catch( IOException e){ e.printStackTrace(); }finally { try{ if(fileReader!=null){ //關閉文件流 fileReader.close(); } }catch(IOException e){ e.printStackTrace(); } } } }
學習任務:字符數(shù)組讀取文件
package com.reader_; import java.io.FileReader; import java.io.IOException; public class ReadFile02 { public static void main(String[] args) { String filePath="e:\\story.txt"; FileReader fileReader=null; int readLen=0; char[] buf=new char[8]; //創(chuàng)建FileReader對象 try{ fileReader =new FileReader(filePath); //循環(huán)讀取 使用 read(buf), 返回的是實際讀取到的字符數(shù) //如果返回-1, 說明到文件結束 while((readLen=fileReader.read(buf))!=-1){ System.out.print(new String(buf,0,readLen)); } }catch( IOException e){ e.printStackTrace(); }finally { try{ if(fileReader!=null){ fileReader.close(); } }catch(IOException e){ e.printStackTrace(); } } } }
學習任務:使用 FileWriter 將 “風雨之后,定見彩虹” 寫入到 note.txt 文件中
package com.hspedu.writer_; import java.io.FileWriter; import java.io.IOException; public class FileWriter_ { public static void main(String[] args) { String filePath="e:\\noth.txt"; FileWriter fileWriter=null; char[] chars={'a','b','c'}; try { fileWriter = new FileWriter(filePath); //3) write(int):寫入單個字符 fileWriter.write('H'); // 4) write(char[]):寫入指定數(shù)組 fileWriter.write(chars); // 5) write(char[],off,len):寫入指定數(shù)組的指定部分 fileWriter.write("程序員飛鳥".toCharArray(), 0, 3); // 6) write(string):寫入整個字符串 fileWriter.write(" 你好廣州"); fileWriter.write("風雨之后,定見彩虹"); // 7) write(string,off,len):寫入字符串的指定部分 fileWriter.write("上海天津", 0, 2); } catch (IOException e) { e.printStackTrace(); }finally{ try { //對應 FileWriter , 一定要關閉流,或者 flush 才能真正的把數(shù)據(jù)寫入文件里面 fileWriter.flush();//關閉文件流 } catch (IOException e) { e.printStackTrace(); } } System.out.println("程序結束~"); } }
到此這篇關于Java細數(shù)IO流基礎到方法使用的文章就介紹到這了,更多相關Java IO流內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
java阿拉伯數(shù)字轉(zhuǎn)中文數(shù)字
這篇文章主要為大家詳細介紹了java實現(xiàn)阿拉伯數(shù)字轉(zhuǎn)換為中文數(shù)字,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04Java springboot 配置文件與多環(huán)境配置與運行優(yōu)先級
這篇文章主要介紹了Java springboot如何配置文件,進行多環(huán)境配置,以及運行優(yōu)先級,感興趣的小伙伴可以借鑒一下2023-04-04java 三種將list轉(zhuǎn)換為map的方法詳解
這篇文章主要介紹了java 三種將list轉(zhuǎn)換為map的方法詳解的相關資料,需要的朋友可以參考下2017-01-01Springboot項目中單元測試時注入bean失敗的解決方案
這篇文章主要介紹了Springboot項目中單元測試時注入bean失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11Springboot中使用Redisson+AOP+自定義注解實現(xiàn)訪問限流與黑名單攔截
本文主要介紹了Springboot中使用Redisson+AOP+自定義注解實現(xiàn)訪問限流與黑名單攔截,包含針對用戶IP限流,整個接口的訪問限流,以及對某個參數(shù)字段的限流,并且支持請求限流后處理回調(diào),感興趣的可以了解一下2024-02-02使用profiles進行多環(huán)境配置的代碼實現(xiàn)
在項目開發(fā)的過程中會用到多個環(huán)境,為了便于開發(fā)使用,通常需要使用profiles進行多環(huán)境配置,所以本文給大家介紹了使用profiles進行多環(huán)境配置的代碼實現(xiàn),需要的朋友可以參考下2024-02-02