日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

7、I/O流

發布時間:2025/7/14 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 7、I/O流 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?一、流的概念:流是一組有順序的有起點和終點字節集合,是對數據傳輸的總稱或抽象。即數據在兩設備間的傳輸稱為流,流的本質是數據傳輸,根據數據傳輸特性將流抽象為各種類,方便更直觀的進行數據操作。I/O就即用于處理設備之間的數據傳輸。

  1、流的分類:

    a)、按操作數據單位分類:字節流字符流

    b)、按流向分類:輸入流、輸出流

    c)、按照功能分類:節點流、處理流。

  

    字節流的抽象基類:InputStream、OutputStream
    字符流的抽象基類:Reader、Writer

二、字符流:Reader、Writer

  1、FileWriter:

1 public class IOTest { 2 3 /** 4 * 需求:往一個文本文件里寫入一些字符、字符串 5 * 1、創建一個FileWriter對象,該對象一被初始化就必須要明確被操作的文件。而且該文件會被創建到指定目錄下,如果該目錄下已有同名文件,將被覆蓋。 6 * 2、調用write方法,將字符串寫入到流(內存)中 7 * 3、調用flush方法刷新緩沖中的數據,將數據刷到目的地中 8 * 4、close方法:關閉流資源,但是關閉之前會刷新一次內存中的緩沖數據,將數據刷新到目的地中 9 * 10 * 注意:close和flush的區別:flush刷新后流可以繼續使用;close刷新后會關閉流 11 */ 12 public static void main(String[] args) { 13 Writer writer = null; 14 try{ 15 /* true:代表打開已存在的文件。如果指定的文件不存在,就會創建新的文件 16 * 如果指定的文件已存在,不會創建新的文件,會在原有數據的基礎上追加新的數據*/ 17 writer = new FileWriter("E:\\qqq.txt", true); 18 writer.append('a'); //將指定字符添加到此 writer 19 writer.append("123qwerewqr");//將指定字符序列添加到此 writer。 20 writer.append("--abcdesafd", 0, 4);// 包含頭,不包含尾 將指定字符序列的子序列添加到此 writer.Appendable 即將"--abcdesafd"的第0-3位上的字符添加到write上 21 char[] ch = new char[]{'A', 'Q', 'W', 'T'}; 22 writer.write(ch);//寫入數組 23 writer.write(ch, 1, 2);//寫入數組的一部分 24 writer.write("adsfkwe\r\n字符流寫入示例");// 記事本里要換行使用\r\n 25 26 writer.flush();// 調用flush方法刷新緩沖中的數據,將數據刷到目的地中 27 28 }catch(Exception e){ 29 e.printStackTrace(); 30 }finally{ 31 try { 32 if(writer != null){ 33 writer.close();// 在關閉流資源之前,會先調用flush方法,然后再關閉 34 } 35 } catch (Exception e) { 36 e.printStackTrace(); 37 } 38 } 39 } 40 } 41 42 運行后可在相應的txt文件中看到下面的類容: 43 44 a123qwerewqr--abAQWTQWadsfkwe 45 字符流寫入示例

  2、FileRead:

    a)、一個一個的讀:

1 public class IOTest { 2 /** 3 * 1、創建一個fileReader對象,和指定的文件想關聯,要保證文件已經存在,如果不存在會報FileNotFoundException錯誤 4 * 2、調用讀取流的read方法,一次讀一個字節,并且會自動往下讀。 5 * 3、調用close方法關閉資源 6 */ 7 8 public static void main(String[] args) { 9 try { 10 FileReader fr = new FileReader("E://qqq.txt"); 11 System.out.println((char)fr.read()); //這個地方一定要加強轉,否則顯示的將是ASCLL碼的值 12 //讀取文件中所有的字符 13 while(fr.ready()){ //如果保證下一個 read() 不阻塞輸入 14 System.out.print((char)fr.read()); 15 } 16 fr.close(); 17 } catch (Exception e) { 18 e.printStackTrace(); 19 } 20 } 21 }

    b)、用數組來一次性提取文件中的類容:

public class IOTest {/*** 1、創建一個fileReader對象,和指定的文件想關聯,要保證文件已經存在,如果不存在會報FileNotFoundException錯誤* 2、調用讀取流的read方法,一次讀一個字節,并且會自動往下讀。* 3、調用close方法關閉資源*/public static void main(String[] args) {try {FileReader fr = new FileReader("E://qqq.txt");char[] buff = new char[4];int num1 = fr.read(buff);// 將字符讀入至buff數組里,返回的是讀取的字符數System.out.println(num1 + "\t" + String.valueOf(buff));int num2 = fr.read(buff);System.out.println(num2 + "\t" + String.valueOf(buff));fr.close();} catch (Exception e) {e.printStackTrace();}} }/* 顯示結果為: 4 a123 4 qwer */

?三、字符流的緩沖區:緩沖區提高了流的操作效率。但必須注意在創建緩沖區之前必須要先有流的對象。

  1、BufferedWriter

1 public class IOTest { 2 public static void main(String[] args) { 3 FileWriter fw = null; 4 BufferedWriter bw = null; 5 try { 6 fw = new FileWriter("E:\\qqq.txt");// 創建一個字符流的輸出對象 7 bw = new BufferedWriter(fw);// 創建一個緩沖區字符流,將字符流對象作為參數傳遞給構造方法 8 bw.write("asdflkj;l123123");// 通過 write方法將內容寫入緩沖區里 9 bw.write("asdflkj;l123123"); 10 bw.newLine();//寫入一個行分隔符。 11 bw.write("asflkj;l123123"); 12 bw.newLine(); 13 bw.write("asdflkj;l123123"); 14 bw.flush();// 通過flush方法將緩沖區里的內容寫入至目標文件里 15 16 } catch (Exception e) { 17 e.printStackTrace(); 18 } finally{ 19 try{ 20 if(bw != null){ 21 bw.close();// 會關閉與之相關的字符流對象 22 } 23 }catch(Exception e){ 24 e.printStackTrace(); 25 } 26 } 27 } 28 /* 29 在qqq.txt文件中輸出結果為: 30 asdflkj;l123123asdflkj;l123123 31 asflkj;l123123 32 asdflkj;l123123 33 * */ 34 }

  2、BufferedReader

1 public class IOTest { 2 public static void main(String[] args) { 3 FileReader fr = null; 4 BufferedReader br = null; 5 try { 6 // 創建一個字符輸入流對象 7 fr = new FileReader("E:\\qqq.txt"); 8 // 創建一個緩沖字符輸入流,將字符輸入流對象作為參數傳遞給構造方法 9 br = new BufferedReader(fr); 10 String str = ""; 11 while((str = br.readLine()) != null){ //readLine():讀取一個文本行 12 System.out.println(str); 13 } 14 }catch (Exception e) { 15 e.printStackTrace(); 16 } finally{ 17 try{ 18 if(br != null){ 19 br.close();// 關閉與之相關的字符流 20 } 21 }catch(Exception e){ 22 e.printStackTrace(); 23 } 24 } 25 } 26 }

?

四、字節流:InputStream、OutputStream

  1、FileInputStream

1 public class IOTest { 2 /** 3 * 讀取a.jpg這幅圖片中的數據在后臺顯示 4 */ 5 public static void main(String[] args) { 6 FileInputStream fis = null; 7 try{ 8 fis = new FileInputStream("E:\\a.jpg"); 9 byte[] buff = new byte[1024]; 10 int len = 0; 11 while((len = fis.read(buff)) != -1){ 12 for(int i = 0; i < len; i++){ 13 System.out.print(buff[i]); 14 } 15 System.out.println(); 16 } 17 18 }catch(Exception e){ 19 e.printStackTrace(); 20 }finally{ 21 try{ 22 if(fis != null){ 23 fis.close(); 24 } 25 }catch(Exception e){ 26 e.printStackTrace(); 27 } 28 } 29 } 30 }

  2、FileOutputStream

1 public class IOTest { 2 /* 3 * 向qqq.txt文件中寫入一個一組數據 4 */ 5 public static void main(String[] args) { 6 FileOutputStream fos = null; 7 try{ 8 fos = new FileOutputStream("E:\\qqq.txt"); 9 byte[] b1 = new byte[]{122, 97, 99, 65, 90}; 10 fos.write(b1);// 通過write方法寫入一個字節數組 11 }catch(Exception e){ 12 e.printStackTrace(); 13 }finally{ 14 try{ 15 if(fos != null){ 16 fos.close(); 17 } 18 }catch(Exception e){ 19 e.printStackTrace(); 20 } 21 } 22 } 23 }

  3、通過字節流及字節流緩沖區復制一個圖片

1 public class IOTest { 2 /*將E盤中的a.jpg圖片復制為b.jpg 3 * 4 */ 5 public static void main(String[] args) { 6 FileInputStream fis = null; 7 FileOutputStream fos = null; 8 try{ 9 fis = new FileInputStream("E:\\b.jpg"); 10 fos = new FileOutputStream("E:\\c.jpg"); 11 byte[] buff = new byte[1024]; 12 int len = 0; 13 while((len = fis.read(buff)) != -1){ 14 for(int i = 0; i < len; i++){ 15 fos.write(buff[i]); 16 } 17 } 18 }catch(Exception e){ 19 e.printStackTrace(); 20 }finally{ 21 try{ 22 if(fis != null)fis.close(); 23 }catch(Exception e){e.printStackTrace();} 24 try{ 25 if(fos != null)fos.close(); 26 }catch(Exception e){e.printStackTrace();} 27 } 28 } 29 }

五、讀取鍵盤錄入:

1 public class IOTest { 2 /* 3 * 讀取鍵盤值 4 */ 5 public static void main(String[] args) throws IOException { 6 //獲取到讀取鍵盤錄入的流對象。類型是InputStream 7 InputStream is = System.in; 8 while(true){ 9 int num1 = is.read();// 返回的是一個ASCLL碼的值 10 System.out.println((char)num1); 11 } 12 13 } 14 }

六、File:將文件或者文件夾封裝成對象,方便對文件與文件夾的屬性信息進行操作。separator:與系統有關的默認名稱分隔符。

  1、對文件的一些常見操作的舉例:

1 public class IOTest { 2 3 public static void main(String[] args) throws IOException { 4 /* 5 * 實驗的空目錄或者說是起始目錄為E:\\99 6 */ 7 File file = new File("E:\\99\\a.txt"); 8 File file1 = new File("E:\\99\\aa"); 9 File file2 = new File("E:\\99\\bb\\cc"); 10 File file3 = new File("E:\\99\\qqq.txt"); 11 /* 12 * 創建文件或這是一個文件夾 13 */ 14 //創建一個"a.txt"文件,創建成功則返回true 15 System.out.println("createNewFile():" + file.createNewFile()); 16 //創建一個名字為"aa"的文件夾 17 System.out.println("createNewFile():" + file1.mkdir()); 18 // 創建多級目錄即在E:\\99目錄下先創建一個bb文件夾,再在bb文件夾下新建一個cc文件夾 19 System.out.println("mkdirs():" + file2.mkdirs()); 20 21 /* 22 * 刪除一個文件或者刪除一個文件夾 23 */ 24 System.out.println("delete():" + file.delete()); 25 System.out.println("delete():" + file1.delete()); 26 System.out.println("delete():" + file2.delete()); 27 /* 28 * 判斷 29 */ 30 //測試此抽象路徑名表示的文件或目錄是否存在 31 System.out.println("exists():" + file.exists()); 32 //測試此抽象路徑名表示的文件是否是一個目錄。 33 System.out.println("isDirectory():" + file.isDirectory()); 34 //測試此抽象路徑名表示的文件是否是一個標準文件。 35 System.out.println("isFile():" + file.isFile()); 36 // 測試此抽象路徑名指定的文件是否是一個隱藏文件。 37 System.out.println("isHidden():" + file.isHidden()); 38 /* 39 * 獲取信息 40 */ 41 //返回由此抽象路徑名表示的文件或目錄的名稱。 42 System.out.println("getName():" + file3.getName()); 43 //返回此抽象路徑名父目錄的路徑名字符串,如果此路徑名沒有指定父目錄,則返回 null。 44 System.out.println("getParent():" + file3.getParent()); 45 //將此抽象路徑名轉換為一個路徑名字符串。 46 System.out.println("getPath():" + file3.getPath()); 47 //返回此抽象路徑名的絕對路徑名字符串。 48 System.out.println("getAbsolutePath():" + file3.getAbsolutePath()); 49 } 50 }

?

轉載于:https://www.cnblogs.com/czj-zhm/p/5853755.html

總結

以上是生活随笔為你收集整理的7、I/O流的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。