成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Java輸入數據流詳解

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

Java輸入數據流

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

Java.io包簡介

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

InputStream類和OutputStream類

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

  1. public abstract read()throws IOException 

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

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

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

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

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

  1. public long skip(long n)throws IOException  

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

  1. public int available()throws IOException  

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

  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] = "信息技術和互聯網(計算機軟硬件,通訊) "+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. * 創建與刪除文件   
  71. * @param filePath   
  72. * @param fileName   
  73. * @return 創建成功返回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("文件已經刪除!");   
  85. }   
  86. else   
  87. {   
  88. file.createNewFile();   
  89. result = true;   
  90. System.out.println("文件已經創建!");   
  91. }   
  92. return result;   
  93. }   
  94. /**   
  95. * 創建和刪除目錄   
  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("目錄已經存在,已刪除!");   
  110. result = true;   
  111. }   
  112. else   
  113. {   
  114. file.mkdir();   
  115. System.out.println("目錄不存在,已經建立!");   
  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+" 文件中沒有數據!");   
  160. }   
  161. else   
  162. {   
  163. System.out.println(fileName+" 文件中有數據!");   
  164. }   
  165. fr.close();   
  166. return result;   
  167. }   
  168. /**   
  169. * 讀取文件中的所有內容   
  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. * 一行一行的讀取文件中的數據   
  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輸入數據流就介紹完了

【編輯推薦】

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

2011-12-14 15:57:13

javanio

2011-08-29 10:19:09

Microsoft S控制較大數據流

2022-03-18 08:57:17

前端數據流選型

2016-11-14 19:01:36

數據流聊天系統web

2009-04-13 16:35:25

TSQL查詢SQL Server

2020-02-06 19:12:36

Java函數式編程編程語言

2013-10-21 10:58:50

微軟大數據SQL Server

2017-11-16 19:26:34

海量數據算法計算機

2009-07-15 09:06:11

Linux圖形系統X11的CS架構

2021-10-27 10:43:36

數據流中位數偶數

2014-02-11 08:51:15

亞馬遜PaaSAppStream

2011-04-14 14:43:38

SSISTransformat

2012-07-30 08:31:08

Storm數據流

2019-12-19 14:38:08

Flink SQL數據流Join

2011-04-19 09:18:02

SSIS數據轉換

2019-06-18 13:51:08

大數據流處理新興市場

2013-10-12 13:14:27

TwitterGoogle大數據

2021-06-08 05:50:00

數據流數字化轉型數字化

2010-04-28 15:52:15

數據流負載均衡

2014-12-02 10:56:47

TCPIP交互數據流
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品一区二区不卡 | 成年人在线 | 亚洲欧美日韩高清 | wwww.8888久久爱站网 | 亚洲永久字幕 | 天天干天天干 | 日本一区二区三区四区 | 欧美在线激情 | 免费在线视频一区二区 | 久草热8精品视频在线观看 午夜伦4480yy私人影院 | 久久久久久久一区 | 国产亚洲精品精品国产亚洲综合 | 综合九九 | 久久人体视频 | 中文字幕视频免费 | 日韩一级黄色毛片 | 中文字幕一区二区三区四区五区 | 国产精品精品久久久 | av永久 | 黄色大片在线免费观看 | 欧美精品欧美精品系列 | 日本特黄a级高清免费大片 成年人黄色小视频 | 欧美精品一区三区 | 日本手机看片 | 国产视频一区二区三区四区五区 | 国产在线精品一区二区 | 人人做人人澡人人爽欧美 | www免费视频| 人人亚洲 | 国产在线视频99 | 99久久免费精品国产免费高清 | 岛国av一区二区三区 | 美女毛片免费看 | 中文字幕日韩三级 | 91看片网 | 日本三级在线网站 | 国产亚洲成av人片在线观看桃 | 日韩中文字幕免费在线 | 天天曰夜夜操 | 亚洲www | 日韩精品在线看 |