7、I/O流
?一、流的概念:流是一組有順序的,有起點(diǎn)和終點(diǎn)的字節(jié)集合,是對數(shù)據(jù)傳輸?shù)目偡Q或抽象。即數(shù)據(jù)在兩設(shè)備間的傳輸稱為流,流的本質(zhì)是數(shù)據(jù)傳輸,根據(jù)數(shù)據(jù)傳輸特性將流抽象為各種類,方便更直觀的進(jìn)行數(shù)據(jù)操作。I/O就即用于處理設(shè)備之間的數(shù)據(jù)傳輸。
1、流的分類:
a)、按操作數(shù)據(jù)單位分類:字節(jié)流、字符流。
b)、按流向分類:輸入流、輸出流。
c)、按照功能分類:節(jié)點(diǎn)流、處理流。
字節(jié)流的抽象基類:InputStream、OutputStream
字符流的抽象基類:Reader、Writer
二、字符流:Reader、Writer
1、FileWriter:
1 public class IOTest { 2 3 /** 4 * 需求:往一個(gè)文本文件里寫入一些字符、字符串 5 * 1、創(chuàng)建一個(gè)FileWriter對象,該對象一被初始化就必須要明確被操作的文件。而且該文件會被創(chuàng)建到指定目錄下,如果該目錄下已有同名文件,將被覆蓋。 6 * 2、調(diào)用write方法,將字符串寫入到流(內(nèi)存)中 7 * 3、調(diào)用flush方法刷新緩沖中的數(shù)據(jù),將數(shù)據(jù)刷到目的地中 8 * 4、close方法:關(guān)閉流資源,但是關(guān)閉之前會刷新一次內(nèi)存中的緩沖數(shù)據(jù),將數(shù)據(jù)刷新到目的地中 9 * 10 * 注意:close和flush的區(qū)別:flush刷新后流可以繼續(xù)使用;close刷新后會關(guān)閉流 11 */ 12 public static void main(String[] args) { 13 Writer writer = null; 14 try{ 15 /* true:代表打開已存在的文件。如果指定的文件不存在,就會創(chuàng)建新的文件 16 * 如果指定的文件已存在,不會創(chuàng)建新的文件,會在原有數(shù)據(jù)的基礎(chǔ)上追加新的數(shù)據(jù)*/ 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);//寫入數(shù)組 23 writer.write(ch, 1, 2);//寫入數(shù)組的一部分 24 writer.write("adsfkwe\r\n字符流寫入示例");// 記事本里要換行使用\r\n 25 26 writer.flush();// 調(diào)用flush方法刷新緩沖中的數(shù)據(jù),將數(shù)據(jù)刷到目的地中 27 28 }catch(Exception e){ 29 e.printStackTrace(); 30 }finally{ 31 try { 32 if(writer != null){ 33 writer.close();// 在關(guān)閉流資源之前,會先調(diào)用flush方法,然后再關(guān)閉 34 } 35 } catch (Exception e) { 36 e.printStackTrace(); 37 } 38 } 39 } 40 } 41 42 運(yùn)行后可在相應(yīng)的txt文件中看到下面的類容: 43 44 a123qwerewqr--abAQWTQWadsfkwe 45 字符流寫入示例2、FileRead:
a)、一個(gè)一個(gè)的讀:
1 public class IOTest { 2 /** 3 * 1、創(chuàng)建一個(gè)fileReader對象,和指定的文件想關(guān)聯(lián),要保證文件已經(jīng)存在,如果不存在會報(bào)FileNotFoundException錯(cuò)誤 4 * 2、調(diào)用讀取流的read方法,一次讀一個(gè)字節(jié),并且會自動往下讀。 5 * 3、調(diào)用close方法關(guān)閉資源 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()); //這個(gè)地方一定要加強(qiáng)轉(zhuǎn),否則顯示的將是ASCLL碼的值 12 //讀取文件中所有的字符 13 while(fr.ready()){ //如果保證下一個(gè) read() 不阻塞輸入 14 System.out.print((char)fr.read()); 15 } 16 fr.close(); 17 } catch (Exception e) { 18 e.printStackTrace(); 19 } 20 } 21 }b)、用數(shù)組來一次性提取文件中的類容:
public class IOTest {/*** 1、創(chuàng)建一個(gè)fileReader對象,和指定的文件想關(guān)聯(lián),要保證文件已經(jīng)存在,如果不存在會報(bào)FileNotFoundException錯(cuò)誤* 2、調(diào)用讀取流的read方法,一次讀一個(gè)字節(jié),并且會自動往下讀。* 3、調(diào)用close方法關(guān)閉資源*/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數(shù)組里,返回的是讀取的字符數(shù)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();}} }/* 顯示結(jié)果為: 4 a123 4 qwer */?三、字符流的緩沖區(qū):緩沖區(qū)提高了流的操作效率。但必須注意在創(chuàng)建緩沖區(qū)之前必須要先有流的對象。
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");// 創(chuàng)建一個(gè)字符流的輸出對象 7 bw = new BufferedWriter(fw);// 創(chuàng)建一個(gè)緩沖區(qū)字符流,將字符流對象作為參數(shù)傳遞給構(gòu)造方法 8 bw.write("asdflkj;l123123");// 通過 write方法將內(nèi)容寫入緩沖區(qū)里 9 bw.write("asdflkj;l123123"); 10 bw.newLine();//寫入一個(gè)行分隔符。 11 bw.write("asflkj;l123123"); 12 bw.newLine(); 13 bw.write("asdflkj;l123123"); 14 bw.flush();// 通過flush方法將緩沖區(qū)里的內(nèi)容寫入至目標(biāo)文件里 15 16 } catch (Exception e) { 17 e.printStackTrace(); 18 } finally{ 19 try{ 20 if(bw != null){ 21 bw.close();// 會關(guān)閉與之相關(guān)的字符流對象 22 } 23 }catch(Exception e){ 24 e.printStackTrace(); 25 } 26 } 27 } 28 /* 29 在qqq.txt文件中輸出結(jié)果為: 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 // 創(chuàng)建一個(gè)字符輸入流對象 7 fr = new FileReader("E:\\qqq.txt"); 8 // 創(chuàng)建一個(gè)緩沖字符輸入流,將字符輸入流對象作為參數(shù)傳遞給構(gòu)造方法 9 br = new BufferedReader(fr); 10 String str = ""; 11 while((str = br.readLine()) != null){ //readLine():讀取一個(gè)文本行 12 System.out.println(str); 13 } 14 }catch (Exception e) { 15 e.printStackTrace(); 16 } finally{ 17 try{ 18 if(br != null){ 19 br.close();// 關(guān)閉與之相關(guān)的字符流 20 } 21 }catch(Exception e){ 22 e.printStackTrace(); 23 } 24 } 25 } 26 }?
四、字節(jié)流:InputStream、OutputStream
1、FileInputStream
1 public class IOTest { 2 /** 3 * 讀取a.jpg這幅圖片中的數(shù)據(jù)在后臺顯示 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文件中寫入一個(gè)一組數(shù)據(jù) 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方法寫入一個(gè)字節(jié)數(shù)組 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、通過字節(jié)流及字節(jié)流緩沖區(qū)復(fù)制一個(gè)圖片
1 public class IOTest { 2 /*將E盤中的a.jpg圖片復(fù)制為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();// 返回的是一個(gè)ASCLL碼的值 10 System.out.println((char)num1); 11 } 12 13 } 14 }六、File:將文件或者文件夾封裝成對象,方便對文件與文件夾的屬性信息進(jìn)行操作。separator:與系統(tǒng)有關(guān)的默認(rèn)名稱分隔符。
1、對文件的一些常見操作的舉例:
1 public class IOTest { 2 3 public static void main(String[] args) throws IOException { 4 /* 5 * 實(shí)驗(yàn)的空目錄或者說是起始目錄為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 * 創(chuàng)建文件或這是一個(gè)文件夾 13 */ 14 //創(chuàng)建一個(gè)"a.txt"文件,創(chuàng)建成功則返回true 15 System.out.println("createNewFile():" + file.createNewFile()); 16 //創(chuàng)建一個(gè)名字為"aa"的文件夾 17 System.out.println("createNewFile():" + file1.mkdir()); 18 // 創(chuàng)建多級目錄即在E:\\99目錄下先創(chuàng)建一個(gè)bb文件夾,再在bb文件夾下新建一個(gè)cc文件夾 19 System.out.println("mkdirs():" + file2.mkdirs()); 20 21 /* 22 * 刪除一個(gè)文件或者刪除一個(gè)文件夾 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 //測試此抽象路徑名表示的文件是否是一個(gè)目錄。 33 System.out.println("isDirectory():" + file.isDirectory()); 34 //測試此抽象路徑名表示的文件是否是一個(gè)標(biāo)準(zhǔn)文件。 35 System.out.println("isFile():" + file.isFile()); 36 // 測試此抽象路徑名指定的文件是否是一個(gè)隱藏文件。 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 //將此抽象路徑名轉(zhuǎn)換為一個(gè)路徑名字符串。 46 System.out.println("getPath():" + file3.getPath()); 47 //返回此抽象路徑名的絕對路徑名字符串。 48 System.out.println("getAbsolutePath():" + file3.getAbsolutePath()); 49 } 50 }?
轉(zhuǎn)載于:https://www.cnblogs.com/czj-zhm/p/5853755.html
總結(jié)
- 上一篇: javascript转换金额格式
- 下一篇: 2016-09-09