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

Java輸入數(shù)據(jù)流詳解

開發(fā) 后端
這里介紹Java輸入數(shù)據(jù)流,InputStream類是所有輸入數(shù)據(jù)流的父類,它是一個抽象類,定義了所有Java輸入數(shù)據(jù)流都具有的共通特性。

Java輸入數(shù)據(jù)流

在Java中,我們把能夠讀取一個字節(jié)序列的對象稱作一個Java輸入數(shù)據(jù)流;而我們把夠寫一個字節(jié)序列稱作一個輸出流。它們分別由抽象類 InputStream和OutputStream類表示。因為面向字節(jié)的流不方便用來處理存儲為Unicode(每個字符使用兩個字節(jié))的信息。所以Java 引入了用來處理Unicode字符的類層次,這些類派生自抽象類Reader和Writer,它們用于讀寫雙字節(jié)的Unicode字符,而不是單字節(jié)字符。

Java.io包簡介

JDK標準幫助文檔是這樣解釋Java.io包的,通過數(shù)據(jù)流、序列和文件系統(tǒng)為系統(tǒng)提供輸入輸出。

InputStream類和OutputStream類

InputStream類是所有輸入數(shù)據(jù)流的父類,它是一個抽象類,定義了所有Java輸入數(shù)據(jù)流都具有的共通特性。
java.io.InputStream的方法如下: 

  1. public abstract read()throws IOException 

 讀取一個字節(jié)并返回該字節(jié),如果到輸入源的末則返回-1。一個具體的Java輸入數(shù)據(jù)流需要重載此方法,以提供 有用的功能。例如:在FileInputStream類中,該方法從一個文件讀取一個字節(jié)。

  1. public int read(byte[] b)throws IOException  

把數(shù)據(jù)讀入到一個字節(jié)數(shù)據(jù)中,并返回實際讀取的字節(jié)數(shù)目。如果遇到流末 則返回-1,該方法最多讀取b.length個字節(jié)。

  1. public abstract int read(byte[] b,int off,int len)throws IOException  

把數(shù)據(jù)讀入到一個字節(jié)數(shù)組中并返回實際讀取的字節(jié)數(shù)目。如果遇到流的末尾則的返回-1。 其中參數(shù)off表示第一個字節(jié)在b中的位置,len表示讀取的最大字節(jié)數(shù)。

  1. public long skip(long n)throws IOException  

略過N個字節(jié)不讀取,會返回實際略過的字節(jié)數(shù)目。因為數(shù)據(jù)流中剩下的數(shù)據(jù)可能不到N 個字節(jié)那么多,所以此時返回值會小于N。

  1. public int available()throws IOException  

read方法(包括后面要講的OutputStream類的Write方法)都能夠陰塞一個線程,直到字節(jié)被 實際讀取或寫入。這意味著如果一個流不能立即被讀或被寫

  1. /*   
  2. * Created on 2005-3-10   
  3. * To change the template for this generated file go to   
  4. * Window>Preferences>Java>Code Generation>Code and Comments   
  5. */   
  6. package mytestfiles;   
  7. import java.io.BufferedReader;   
  8. import java.io.File;   
  9. import java.io.FileReader;   
  10. import java.io.FileWriter;   
  11. import java.io.IOException;   
  12. import java.io.PrintWriter;   
  13.  
  14. /**   
  15. * @author zhangqinglin   
  16. * To change the template for this generated type comment go to   
  17. * Window>Preferences>Java>Code Generation>Code and Comments   
  18. */   
  19. public class Files   
  20. {   
  21. public static void main(String[] args) throws IOException   
  22. {   
  23. Files f = new Files();   
  24. // System.out.println(f.readFile("f:\\LinkFile.java"));   
  25. // f.readAllFile("f:\\","LinkFile.java");   
  26. // f.readLineFile("f:\\","LinkFile.java");   
  27. // System.out.println(f.fileIsNull("f:\\","122.txt"));   
  28. // f.readFolderByFile("F:\\PDF");   
  29. // System.out.println(f.createAndDeleteFolder("ss","f:\\"));   
  30. // System.out.println(f.createAndDeleteFile("f:\\ss\\","TestFile.dat"));   
  31. String[] ss = new String[50];   
  32. for(int i=0;i{   
  33. ss[i] = "信息技術和互聯(lián)網(wǎng)(計算機軟硬件,通訊) "+i;   
  34. }   
  35. f.writeFile("f:\\ss\\","TestFile.txt",ss);   
  36. }   
  37. /**   
  38. * 文件的寫入   
  39. * @param filePath(文件路徑)   
  40. * @param fileName(文件名)   
  41. * @param args[]   
  42. * @throws IOException   
  43. */   
  44. public void writeFile(String filePath,String fileName,String[] args) throws IOException   
  45. {   
  46. FileWriter fw = new FileWriter(filePath+fileName);   
  47. PrintWriter out=new PrintWriter(fw);   
  48. for(int i=0;i{   
  49. out.write(args[i]);   
  50. out.println();   
  51. out.flush();   
  52. }   
  53. fw.close();   
  54. out.close();   
  55. }   
  56. /**   
  57. * 文件的寫入   
  58. * @param filePath(文件路徑)   
  59. * @param fileName(文件名)   
  60. * @param args   
  61. * @throws IOException   
  62. */   
  63. public void writeFile(String filePath,String fileName,String args) throws IOException   
  64. {   
  65. FileWriter fw = new FileWriter(filePath+fileName);   
  66. fw.write(args);   
  67. fw.close();   
  68. }   
  69. /**   
  70. * 創(chuàng)建與刪除文件   
  71. * @param filePath   
  72. * @param fileName   
  73. * @return 創(chuàng)建成功返回true   
  74. * @throws IOException   
  75. */   
  76. public boolean createAndDeleteFile(String filePath,String fileName) throws IOException   
  77. {   
  78. boolean result = false;   
  79. File file = new File(filePath,fileName);   
  80. if(file.exists())   
  81. {   
  82. file.delete();   
  83. result = true;   
  84. System.out.println("文件已經(jīng)刪除!");   
  85. }   
  86. else   
  87. {   
  88. file.createNewFile();   
  89. result = true;   
  90. System.out.println("文件已經(jīng)創(chuàng)建!");   
  91. }   
  92. return result;   
  93. }   
  94. /**   
  95. * 創(chuàng)建和刪除目錄   
  96. * @param folderName   
  97. * @param filePath   
  98. * @return 刪除成功返回true   
  99. */   
  100. public boolean createAndDeleteFolder(String folderName,String filePath)   
  101. {   
  102. boolean result = false;   
  103. try   
  104. {   
  105. File file = new File(filePath+folderName);   
  106. if(file.exists())   
  107. {   
  108. file.delete();   
  109. System.out.println("目錄已經(jīng)存在,已刪除!");   
  110. result = true;   
  111. }   
  112. else   
  113. {   
  114. file.mkdir();   
  115. System.out.println("目錄不存在,已經(jīng)建立!");   
  116. result = true;   
  117. }   
  118. }   
  119. catch(Exception ex)   
  120. {   
  121. result = false;   
  122. System.out.println("CreateAndDeleteFolder is error:"+ex);   
  123. }   
  124. return result;   
  125. }   
  126. /**   
  127. * 輸出目錄中的所有文件及目錄名字   
  128. * @param filePath   
  129. */   
  130. public void readFolderByFile(String filePath)   
  131. {   
  132. File file = new File(filePath);   
  133. File[] tempFile = file.listFiles();   
  134. for(int i = 0;i{   
  135. if(tempFile[i].isFile())   
  136. {   
  137. System.out.println("File : "+tempFile[i].getName());   
  138. }   
  139. if(tempFile[i].isDirectory())   
  140. {   
  141. System.out.println("Directory : "+tempFile[i].getName());   
  142. }   
  143. }   
  144. }   
  145. /**   
  146. * 檢查文件中是否為一個空   
  147. * @param filePath   
  148. * @param fileName   
  149. * @return 為空返回true   
  150. * @throws IOException   
  151. */   
  152. public boolean fileIsNull(String filePath,String fileName) throws IOException   
  153. {   
  154. boolean result = false;   
  155. FileReader fr = new FileReader(filePath+fileName);   
  156. if(fr.read() == -1)   
  157. {   
  158. result = true;   
  159. System.out.println(fileName+" 文件中沒有數(shù)據(jù)!");   
  160. }   
  161. else   
  162. {   
  163. System.out.println(fileName+" 文件中有數(shù)據(jù)!");   
  164. }   
  165. fr.close();   
  166. return result;   
  167. }   
  168. /**   
  169. * 讀取文件中的所有內(nèi)容   
  170. * @param filePath   
  171. * @param fileName   
  172. * @throws IOException   
  173. */   
  174. public void readAllFile(String filePath,String fileName) throws IOException   
  175. {   
  176. FileReader fr = new FileReader(filePath+fileName);   
  177. int count = fr.read();   
  178. while(count != -1)   
  179. {   
  180. System.out.print((char)count);   
  181. count = fr.read();   
  182. if(count == 13)   
  183. {   
  184. fr.skip(1);   
  185. }   
  186. }   
  187. fr.close();   
  188. }   
  189. /**   
  190. * 一行一行的讀取文件中的數(shù)據(jù)   
  191. * @param filePath   
  192. * @param fileName   
  193. * @throws IOException   
  194. */   
  195. public void readLineFile(String filePath,String fileName) throws IOException   
  196. {   
  197. FileReader fr = new FileReader(filePath+fileName);   
  198. BufferedReader br = new BufferedReader(fr);   
  199. String line = br.readLine();   
  200. while(line != null)   
  201. {   
  202. System.out.println(line);   
  203. line = br.readLine();   
  204. }   
  205. br.close();   
  206. fr.close();   
  207. }   
  208. }  

到這里Java輸入數(shù)據(jù)流就介紹完了

【編輯推薦】

  1. Java參數(shù)傳遞機制分析:值與引用
  2. 從Java走進Scala:一步步教你使用Scala Actor
  3. Java新型垃圾回收器G1深入探索
  4. Java的synchronized關鍵字:同步機制總結
  5. Java語法技巧之雙括弧初始化
責任編輯:彭凡 來源: Java中文網(wǎng)
相關推薦

2011-12-14 15:57:13

javanio

2011-08-29 10:19:09

Microsoft S控制較大數(shù)據(jù)流

2022-03-18 08:57:17

前端數(shù)據(jù)流選型

2016-11-14 19:01:36

數(shù)據(jù)流聊天系統(tǒng)web

2020-02-06 19:12:36

Java函數(shù)式編程編程語言

2009-04-13 16:35:25

TSQL查詢SQL Server

2017-11-16 19:26:34

海量數(shù)據(jù)算法計算機

2009-07-15 09:06:11

Linux圖形系統(tǒng)X11的CS架構

2013-10-21 10:58:50

微軟大數(shù)據(jù)SQL Server

2021-10-27 10:43:36

數(shù)據(jù)流中位數(shù)偶數(shù)

2014-02-11 08:51:15

亞馬遜PaaSAppStream

2011-04-14 14:43:38

SSISTransformat

2012-07-30 08:31:08

Storm數(shù)據(jù)流

2019-12-19 14:38:08

Flink SQL數(shù)據(jù)流Join

2011-04-19 09:18:02

SSIS數(shù)據(jù)轉換

2019-06-18 13:51:08

大數(shù)據(jù)流處理新興市場

2010-04-28 15:52:15

數(shù)據(jù)流負載均衡

2014-12-02 10:56:47

TCPIP交互數(shù)據(jù)流

2010-04-30 09:53:34

Unix系統(tǒng)

2021-06-08 05:50:00

數(shù)據(jù)流數(shù)字化轉型數(shù)字化
點贊
收藏

51CTO技術棧公眾號