Java输入数据流详解

Java输入数据流

创新互联建站是一家以成都网站建设、网页设计、品牌设计、软件运维、成都网站营销、小程序App开发等移动开发为一体互联网公司。已累计为垃圾桶等众行业中小客户提供优质的互联网建站和软件开发服务。

在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. * @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. { 
  20. public static void main(String[] args) throws IOException 
  21. { 
  22. Files f = new Files(); 
  23. // System.out.println(f.readFile("f:\\LinkFile.java")); 
  24. // f.readAllFile("f:\\","LinkFile.java"); 
  25. // f.readLineFile("f:\\","LinkFile.java"); 
  26. // System.out.println(f.fileIsNull("f:\\","122.txt")); 
  27. // f.readFolderByFile("F:\\PDF"); 
  28. // System.out.println(f.createAndDeleteFolder("ss","f:\\")); 
  29. // System.out.println(f.createAndDeleteFile("f:\\ss\\","TestFile.dat")); 
  30. String[] ss = new String[50]; 
  31. for(int i=0;i{ 
  32. ss[i] = "信息技术和互联网(计算机软硬件,通讯) "+i; 
  33. } 
  34. f.writeFile("f:\\ss\\","TestFile.txt",ss); 
  35. } 
  36. /** 
  37. * 文件的写入 
  38. * @param filePath(文件路径) 
  39. * @param fileName(文件名) 
  40. * @param args[] 
  41. * @throws IOException 
  42. */ 
  43. public void writeFile(String filePath,String fileName,String[] args) throws IOException 
  44. { 
  45. FileWriter fw = new FileWriter(filePath+fileName); 
  46. PrintWriter out=new PrintWriter(fw); 
  47. for(int i=0;i{ 
  48. out.write(args[i]); 
  49. out.println(); 
  50. out.flush(); 
  51. } 
  52. fw.close(); 
  53. out.close(); 
  54. } 
  55. /** 
  56. * 文件的写入 
  57. * @param filePath(文件路径) 
  58. * @param fileName(文件名) 
  59. * @param args 
  60. * @throws IOException 
  61. */ 
  62. public void writeFile(String filePath,String fileName,String args) throws IOException 
  63. { 
  64. FileWriter fw = new FileWriter(filePath+fileName); 
  65. fw.write(args); 
  66. fw.close(); 
  67. } 
  68. /** 
  69. * 创建与删除文件 
  70. * @param filePath 
  71. * @param fileName 
  72. * @return 创建成功返回true 
  73. * @throws IOException 
  74. */ 
  75. public boolean createAndDeleteFile(String filePath,String fileName) throws IOException 
  76. { 
  77. boolean result = false; 
  78. File file = new File(filePath,fileName); 
  79. if(file.exists()) 
  80. { 
  81. file.delete(); 
  82. result = true; 
  83. System.out.println("文件已经删除!"); 
  84. } 
  85. else 
  86. { 
  87. file.createNewFile(); 
  88. result = true; 
  89. System.out.println("文件已经创建!"); 
  90. } 
  91. return result; 
  92. } 
  93. /** 
  94. * 创建和删除目录 
  95. * @param folderName 
  96. * @param filePath 
  97. * @return 删除成功返回true 
  98. */ 
  99. public boolean createAndDeleteFolder(String folderName,String filePath) 
  100. { 
  101. boolean result = false; 
  102. try 
  103. { 
  104. File file = new File(filePath+folderName); 
  105. if(file.exists()) 
  106. { 
  107. file.delete(); 
  108. System.out.println("目录已经存在,已删除!"); 
  109. result = true; 
  110. } 
  111. else 
  112. { 
  113. file.mkdir(); 
  114. System.out.println("目录不存在,已经建立!"); 
  115. result = true; 
  116. } 
  117. } 
  118. catch(Exception ex) 
  119. { 
  120. result = false; 
  121. System.out.println("CreateAndDeleteFolder is error:"+ex); 
  122. } 
  123. return result; 
  124. } 
  125. /** 
  126. * 输出目录中的所有文件及目录名字 
  127. * @param filePath 
  128. */ 
  129. public void readFolderByFile(String filePath) 
  130. { 
  131. File file = new File(filePath); 
  132. File[] tempFile = file.listFiles(); 
  133. for(int i = 0;i{ 
  134. if(tempFile[i].isFile()) 
  135. { 
  136. System.out.println("File : "+tempFile[i].getName()); 
  137. } 
  138. if(tempFile[i].isDirectory()) 
  139. { 
  140. System.out.println("Directory : "+tempFile[i].getName()); 
  141. } 
  142. } 
  143. } 
  144. /** 
  145. * 检查文件中是否为一个空 
  146. * @param filePath 
  147. * @param fileName 
  148. * @return 为空返回true 
  149. * @throws IOException 
  150. */ 
  151. public boolean fileIsNull(String filePath,String fileName) throws IOException 
  152. { 
  153. boolean result = false; 
  154. FileReader fr = new FileReader(filePath+fileName); 
  155. if(fr.read() == -1) 
  156. { 
  157. result = true; 
  158. System.out.println(fileName+" 文件中没有数据!"); 
  159. } 
  160. else 
  161. { 
  162. System.out.println(fileName+" 文件中有数据!"); 
  163. } 
  164. fr.close(); 
  165. return result; 
  166. } 
  167. /** 
  168. * 读取文件中的所有内容 
  169. * @param filePath 
  170. * @param fileName 
  171. * @throws IOException 
  172. */ 
  173. public void readAllFile(String filePath,String fileName) throws IOException 
  174. { 
  175. FileReader fr = new FileReader(filePath+fileName); 
  176. int count = fr.read(); 
  177. while(count != -1) 
  178. { 
  179. System.out.print((char)count); 
  180. count = fr.read(); 
  181. if(count == 13) 
  182. { 
  183. fr.skip(1); 
  184. } 
  185. } 
  186. fr.close(); 
  187. } 
  188. /** 
  189. * 一行一行的读取文件中的数据 
  190. * @param filePath 
  191. * @param fileName 
  192. * @throws IOException 
  193. */ 
  194. public void readLineFile(String filePath,String fileName) throws IOException 
  195. { 
  196. FileReader fr = new FileReader(filePath+fileName); 
  197. BufferedReader br = new BufferedReader(fr); 
  198. String line = br.readLine(); 
  199. while(line != null) 
  200. { 
  201. System.out.println(line); 
  202. line = br.readLine(); 
  203. } 
  204. br.close(); 
  205. fr.close(); 
  206. } 
  207. }

到这里Java输入数据流就介绍完了


分享名称:Java输入数据流详解
文章URL:http://wkslyf.com/article/djecjjo.html