日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Java輸入數(shù)據(jù)流詳解

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

創(chuàng)新互聯(lián)建站是一家以成都網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、品牌設(shè)計(jì)、軟件運(yùn)維、成都網(wǎng)站營(yíng)銷、小程序App開發(fā)等移動(dòng)開發(fā)為一體互聯(lián)網(wǎng)公司。已累計(jì)為垃圾桶等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開發(fā)服務(wù)。

在Java中,我們把能夠讀取一個(gè)字節(jié)序列的對(duì)象稱作一個(gè)Java輸入數(shù)據(jù)流;而我們把夠?qū)懸粋€(gè)字節(jié)序列稱作一個(gè)輸出流。它們分別由抽象類 InputStream和OutputStream類表示。因?yàn)槊嫦蜃止?jié)的流不方便用來(lái)處理存儲(chǔ)為Unicode(每個(gè)字符使用兩個(gè)字節(jié))的信息。所以Java 引入了用來(lái)處理Unicode字符的類層次,這些類派生自抽象類Reader和Writer,它們用于讀寫雙字節(jié)的Unicode字符,而不是單字節(jié)字符。

Java.io包簡(jiǎn)介

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

InputStream類和OutputStream類

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

 
 
 
  1. public abstract read()throws IOException

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

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

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

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

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

 
 
 
  1. public long skip(long n)throws IOException 

略過(guò)N個(gè)字節(jié)不讀取,會(huì)返回實(shí)際略過(guò)的字節(jié)數(shù)目。因?yàn)閿?shù)據(jù)流中剩下的數(shù)據(jù)可能不到N 個(gè)字節(jié)那么多,所以此時(shí)返回值會(huì)小于N。

 
 
 
  1. public int available()throws IOException 

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

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

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


文章題目:Java輸入數(shù)據(jù)流詳解
本文來(lái)源:http://www.dlmjj.cn/article/djecjjo.html