自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

揭開Java 語言中的IO系統(tǒng)的神秘面紗

開發(fā) 后端
本文介紹的是JAVA中的IO系統(tǒng)。我們都知道IO包括文件讀寫,標(biāo)準(zhǔn)設(shè)備輸出等方面。希望本文的介紹能夠給你帶來幫助,一起來看。

Java的核心庫java.io提供了全面的IO接口,包括:文件讀寫,標(biāo)準(zhǔn)設(shè)備輸出等等。Java中IO是以流為基礎(chǔ)進(jìn)行輸入輸出的,所有數(shù)據(jù)被串行化寫入輸出流,或者從輸入流讀入。在具體使用中很多初學(xué)者對Java.io包的使用非常含糊,本文將詳細(xì)解說關(guān)于Java.io的使用。

Input和Output

1. stream代表的是任何有能力產(chǎn)出數(shù)據(jù)的數(shù)據(jù)源,或是任何有能力接收數(shù)據(jù)的接收源。在Java的IO系統(tǒng)中,所有的stream(包括Input和Out stream)都包括兩種類型:

1.1 以字節(jié)為導(dǎo)向的stream

以字節(jié)為導(dǎo)向的stream,表示以字節(jié)為單位從stream中讀取或往stream中寫入信息。以字節(jié)為導(dǎo)向的stream包括下面幾種類型:

input

stream:

1) ByteArrayInputStream:把內(nèi)存中的一個緩沖區(qū)作為InputStream使用

2) StringBufferInputStream:把一個String對象作為InputStream

3) FileInputStream:把一個文件作為InputStream,實現(xiàn)對文件的讀取操作

4) PipedInputStream:實現(xiàn)了pipe的概念,主要在線程中使用

5) SequenceInputStream:把多個InputStream合并為一個InputStream

Out

stream

1) ByteArrayOutputStream:把信息存入內(nèi)存中的一個緩沖區(qū)中

2) FileOutputStream:把信息存入文件中

3) PipedOutputStream:實現(xiàn)了pipe的概念,主要在線程中使用

4) SequenceOutputStream:把多個OutStream合并為一個OutStream

1.2 以Unicode字符為導(dǎo)向的stream

以Unicode字符為導(dǎo)向的stream,表示以Unicode字符為單位從stream中讀取或往stream中寫入信息。以Unicode字符為導(dǎo)向的stream包括下面幾種類型:

Input

Stream

1) CharArrayReader:與ByteArrayInputStream對應(yīng)

2) StringReader:與StringBufferInputStream對應(yīng)

3) FileReader:與FileInputStream對應(yīng)

4) PipedReader:與PipedInputStream對應(yīng)

Out

Stream

1) CharArrayWrite:與ByteArrayOutputStream對應(yīng)

2) StringWrite:無與之對應(yīng)的以字節(jié)為導(dǎo)向的stream

3) FileWrite:與FileOutputStream對應(yīng)

4) PipedWrite:與PipedOutputStream對應(yīng)

以字符為導(dǎo)向的stream基本上對有與之相對應(yīng)的以字節(jié)為導(dǎo)向的stream。兩個對應(yīng)類實現(xiàn)的功能相同,字是在操作時的導(dǎo)向不同。如 CharArrayReader:和ByteArrayInputStream的作用都是把內(nèi)存中的一個緩沖區(qū)作為InputStream使用,所不同的 是前者每次從內(nèi)存中讀取一個字節(jié)的信息,而后者每次從內(nèi)存中讀取一個字符。

1.3 兩種不現(xiàn)導(dǎo)向的stream之間的轉(zhuǎn)換

InputStreamReader和OutputStreamReader:把一個以字節(jié)為導(dǎo)向的stream轉(zhuǎn)換成一個以字符為導(dǎo)向的stream。

2. stream添加屬性

2.1 “為stream添加屬性”的作用

運用上面介紹的Java中操作IO的API,我們就可完成我們想完成的任何操作了。但通過FilterInputStream和FilterOutStream的子類,我們可以為stream添加屬性。下面以一個例子來說明這種功能的作用。

如果我們要往一個文件中寫入數(shù)據(jù),我們可以這樣操作:

FileOutStream fs = new FileOutStream(“test.txt”);

然后就可以通過產(chǎn)生的fs對象調(diào)用write()函數(shù)來往test.txt文件中寫入數(shù)據(jù)了。但是,如果我們想實現(xiàn)“先把要寫入文件的數(shù)據(jù)先緩存到內(nèi)存 中,再把緩存中的數(shù)據(jù)寫入文件中”的功能時,上面的API就沒有一個能滿足我們的需求了。但是通過FilterInputStream和 FilterOutStream的子類,為FileOutStream添加我們所需要的功能。

2.2 FilterInputStream的各種類型

2.2.1 用于封裝以字節(jié)為導(dǎo)向的InputStream

1) DataInputStream:從stream中讀取基本類型(int、char等)數(shù)據(jù)。

2) BufferedInputStream:使用緩沖區(qū)

3) LineNumberInputStream:會記錄input stream內(nèi)的行數(shù),然后可以調(diào)用getLineNumber()和setLineNumber(int)

4) PushbackInputStream:很少用到,一般用于編譯器開發(fā)

2.2.2 用于封裝以字符為導(dǎo)向的InputStream

1) 沒有與DataInputStream對應(yīng)的類。除非在要使用readLine()時改用BufferedReader,否則使用DataInputStream

2) BufferedReader:與BufferedInputStream對應(yīng)

3) LineNumberReader:與LineNumberInputStream對應(yīng)

4) PushBackReader:與PushbackInputStream對應(yīng)

2.3 FilterOutStream的各種類型

2.2.3 用于封裝以字節(jié)為導(dǎo)向的OutputStream

1) DataIOutStream:往stream中輸出基本類型(int、char等)數(shù)據(jù)。

2) BufferedOutStream:使用緩沖區(qū)

3) PrintStream:產(chǎn)生格式化輸出

2.2.4 用于封裝以字符為導(dǎo)向的OutputStream

1) BufferedWrite:與對應(yīng)

2) PrintWrite:與對應(yīng)

3. RandomAccessFile

1) 可通過RandomAccessFile對象完成對文件的讀寫操作

2) 在產(chǎn)生一個對象時,可指明要打開的文件的性質(zhì):r,只讀;w,只寫;rw可讀寫

3) 可以直接跳到文件中指定的位置

4. I/O應(yīng)用的一個例子

java 代碼

 

  1. import java.io.*;   
  2. public class TestIO{   
  3.  public static void main(String[] args)   
  4.  throws IOException{   
  5.  //1.以行為單位從一個文件讀取數(shù)據(jù)   
  6.  BufferedReader in = new BufferedReader(   
  7. new FileReader("F:\\nepalon\\TestIO.java"));   
  8.  String s, s2 = new String();   
  9.  while((s = in.readLine()) != null)   
  10.  s2 += s + "\n";   
  11.  in.close();   
  12.  //1b. 接收鍵盤的輸入   
  13.  BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));   
  14.  System.out.println("Enter a line:");   
  15.  System.out.println(stdin.readLine());   
  16.  //2. 從一個String對象中讀取數(shù)據(jù)   
  17.  StringReader in2 = new StringReader(s2);   
  18.  int c;   
  19.  while((c = in2.read()) != -1)   
  20.  System.out.println((char)c);   
  21.  in2.close();   
  22.  //3. 從內(nèi)存取出格式化輸入   
  23.  try{   
  24. DataInputStream in3 =new DataInputStream(new ByteArrayInputStream(s2.getBytes()));   
  25. while(true)   
  26.  System.out.println((char)in3.readByte());   
  27.  }   
  28.  catch(EOFException e){   
  29. System.out.println("End of stream");   
  30.  }   
  31.  //4. 輸出到文件   
  32.  try{   
  33. BufferedReader in4 =new BufferedReader(new StringReader(s2));   
  34. PrintWriter out1 =new PrintWriter(new BufferedWriter(new FileWriter("F:\\nepalon\\ TestIO.out")));   
  35. int lineCount = 1;   
  36. while((s = in4.readLine()) != null)   
  37.  out1.println(lineCount++ + ":" + s);   
  38.  out1.close();   
  39.  in4.close();   
  40.  }   
  41.  catch(EOFException ex){   
  42. System.out.println("End of stream");   
  43.  }   
  44.  //5. 數(shù)據(jù)的存儲和恢復(fù)   
  45.  try{   
  46. DataOutputStream out2 =new DataOutputStream(new BufferedOutputStream(   
  47.  new FileOutputStream("F:\\nepalon\\ Data.txt")));   
  48. out2.writeDouble(3.1415926);   
  49. out2.writeChars("\nThas was pi:writeChars\n");   
  50. out2.writeBytes("Thas was pi:writeByte\n");   
  51. out2.close();   
  52. DataInputStream in5 =new DataInputStream(   
  53.  new BufferedInputStream(new FileInputStream("F:\\nepalon\\ Data.txt")));   
  54.  BufferedReader in5br =new BufferedReader(new InputStreamReader(in5));   
  55.  System.out.println(in5.readDouble());   
  56.  System.out.println(in5br.readLine());   
  57.  System.out.println(in5br.readLine());   
  58.  }   
  59.  catch(EOFException e){   
  60. System.out.println("End of stream");   
  61.  }   
  62.  //6. 通過RandomAccessFile操作文件   
  63.  RandomAccessFile rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat""rw");   
  64.  for(int i=0; i <10; i++)   
  65. rf.writeDouble(i*1.414);   
  66. rf.close();   
  67. rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat""r");   
  68. for(int i=0; i <10; i++)   
  69.  System.out.println("Value " + i + ":" + rf.readDouble());   
  70.  rf.close();   
  71.  rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat""rw");   
  72.  rf.seek(5*8);   
  73.  rf.writeDouble(47.0001);   
  74.  rf.close();   
  75.  rf = new RandomAccessFile("F:\\nepalon\\ rtest.dat""r");   
  76.  for(int i=0; i <10; i++)   
  77. System.out.println("Value " + i + ":" + rf.readDouble());   
  78. rf.close();   
  79. }   
  80.  } 

 

關(guān)于代碼的解釋(以區(qū)為單位):

1區(qū)中,當(dāng)讀取文件時,先把文件內(nèi)容讀到緩存中,當(dāng)調(diào)用in.readLine()時,再從緩存中以字符的方式讀取數(shù)據(jù)(以下簡稱“緩存字節(jié)讀取方式”)。

1b區(qū)中,由于想以緩存字節(jié)讀取方式從標(biāo)準(zhǔn)IO(鍵盤)中讀取數(shù)據(jù),所以要先把標(biāo)準(zhǔn)IO(System.in)轉(zhuǎn)換成字符導(dǎo)向的stream,再進(jìn)行BufferedReader封裝。

2區(qū)中,要以字符的形式從一個String對象中讀取數(shù)據(jù),所以要產(chǎn)生一個StringReader類型的stream。 

希望通過本文的介紹,能夠給你帶來幫助。

【編輯推薦】

  1. 簡單介紹Java 網(wǎng)絡(luò)程序
  2. JAVA基礎(chǔ)之java運算符大百科
  3. 深入解讀JavaScript內(nèi)存回收機(jī)制
  4. java基礎(chǔ)之如何學(xué)習(xí)java程序設(shè)計
  5. java基礎(chǔ)之談學(xué)習(xí)路線
責(zé)任編輯:于鐵 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2015-08-20 13:43:17

NFV網(wǎng)絡(luò)功能虛擬化

2010-05-17 09:13:35

2014-03-12 11:11:39

Storage vMo虛擬機(jī)

2021-06-07 08:18:12

云計算云端阿里云

2024-12-12 09:40:27

Strace命令監(jiān)控

2009-06-01 09:04:44

Google WaveWeb

2018-03-01 09:33:05

軟件定義存儲

2010-05-26 19:12:41

SVN沖突

2016-04-06 09:27:10

runtime解密學(xué)習(xí)

2009-09-15 15:34:33

Google Fast

2023-11-02 09:55:40

2010-05-11 10:19:17

VMforceJava云計算

2020-09-27 08:02:47

操作系統(tǒng)

2016-11-16 09:06:59

2024-02-14 09:00:00

機(jī)器學(xué)習(xí)索引ChatGPT

2025-01-07 15:07:13

2020-04-14 10:44:01

區(qū)塊鏈滲透測試比特幣

2021-09-17 15:54:41

深度學(xué)習(xí)機(jī)器學(xué)習(xí)人工智能

2010-06-17 10:53:25

桌面虛擬化

2011-08-02 08:59:53

點贊
收藏

51CTO技術(shù)棧公眾號