A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 狼牙 中级黑马   /  2013-7-27 08:17  /  1432 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. 读取文件方法大全
  2. 1、按字节读取文件内容
  3. 2、按字符读取文件内容
  4. 3、按行读取文件内容
  5. 4、随机读取文件内容
  6. public class ReadFromFile {
  7. /**
  8. * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
  9. */
  10. public static void readFileByBytes(String fileName) {
  11. File file = new File(fileName);
  12. InputStream in = null;
  13. try {
  14. System.out.println("以字节为单位读取文件内容,一次读一个字节:");
  15. // 一次读一个字节
  16. in = new FileInputStream(file);
  17. int tempbyte;
  18. while ((tempbyte = in.read()) != -1) {
  19. System.out.write(tempbyte);
  20. }
  21. in.close();
  22. } catch (IOException e) {
  23. e.printStackTrace();
  24. return;
  25. }
  26. try {
  27. System.out.println("以字节为单位读取文件内容,一次读多个字节:");
  28. // 一次读多个字节
  29. byte[] tempbytes = new byte[100];
  30. int byteread = 0;
  31. in = new FileInputStream(fileName);
  32. ReadFromFile.showAvailableBytes(in);
  33. // 读入多个字节到字节数组中,byteread为一次读入的字节数
  34. while ((byteread = in.read(tempbytes)) != -1) {
  35. System.out.write(tempbytes, 0, byteread);
  36. }
  37. } catch (Exception e1) {
  38. e1.printStackTrace();
  39. } finally {
  40. if (in != null) {
  41. try {
  42. in.close();
  43. } catch (IOException e1) {
  44. }
  45. }
  46. }
  47. }

  48. /**
  49. * 以字符为单位读取文件,常用于读文本,数字等类型的文件
  50. */
  51. public static void readFileByChars(String fileName) {
  52. File file = new File(fileName);
  53. Reader reader = null;
  54. try {
  55. System.out.println("以字符为单位读取文件内容,一次读一个字节:");
  56. // 一次读一个字符
  57. reader = new InputStreamReader(new FileInputStream(file));
  58. int tempchar;
  59. while ((tempchar = reader.read()) != -1) {
  60. // 对于windows下,\r\n这两个字符在一起时,表示一个换行。
  61. // 但如果这两个字符分开显示时,会换两次行。
  62. // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
  63. if (((char) tempchar) != '\r') {
  64. System.out.print((char) tempchar);
  65. }
  66. }
  67. reader.close();
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. }
  71. try {
  72. System.out.println("以字符为单位读取文件内容,一次读多个字节:");
  73. // 一次读多个字符
  74. char[] tempchars = new char[30];
  75. int charread = 0;
  76. reader = new InputStreamReader(new FileInputStream(fileName));
  77. // 读入多个字符到字符数组中,charread为一次读取字符数
  78. while ((charread = reader.read(tempchars)) != -1) {
  79. // 同样屏蔽掉\r不显示
  80. if ((charread == tempchars.length)
  81. && (tempchars[tempchars.length - 1] != '\r')) {
  82. System.out.print(tempchars);
  83. } else {
  84. for (int i = 0; i < charread; i++) {
  85. if (tempchars == '\r') {
  86. continue;
  87. } else {
  88. System.out.print(tempchars);
  89. }
  90. }
  91. }
  92. }

  93. } catch (Exception e1) {
  94. e1.printStackTrace();
  95. } finally {
  96. if (reader != null) {
  97. try {
  98. reader.close();
  99. } catch (IOException e1) {
  100. }
  101. }
  102. }
  103. }

  104. /**
  105. * 以行为单位读取文件,常用于读面向行的格式化文件
  106. */
  107. public static void readFileByLines(String fileName) {
  108. File file = new File(fileName);
  109. BufferedReader reader = null;
  110. try {
  111. System.out.println("以行为单位读取文件内容,一次读一整行:");
  112. reader = new BufferedReader(new FileReader(file));
  113. String tempString = null;
  114. int line = 1;
  115. // 一次读入一行,直到读入null为文件结束
  116. while ((tempString = reader.readLine()) != null) {
  117. // 显示行号
  118. System.out.println("line " + line + ": " + tempString);
  119. line++;
  120. }
  121. reader.close();
  122. } catch (IOException e) {
  123. e.printStackTrace();
  124. } finally {
  125. if (reader != null) {
  126. try {
  127. reader.close();
  128. } catch (IOException e1) {
  129. }
  130. }
  131. }
  132. }

  133. /**
  134. * 随机读取文件内容
  135. */
  136. public static void readFileByRandomAccess(String fileName) {
  137. RandomAccessFile randomFile = null;
  138. try {
  139. System.out.println("随机读取一段文件内容:");
  140. // 打开一个随机访问文件流,按只读方式
  141. randomFile = new RandomAccessFile(fileName, "r");
  142. // 文件长度,字节数
  143. long fileLength = randomFile.length();
  144. // 读文件的起始位置
  145. int beginIndex = (fileLength > 4) ? 4 : 0;
  146. // 将读文件的开始位置移到beginIndex位置。
  147. randomFile.seek(beginIndex);
  148. byte[] bytes = new byte[10];
  149. int byteread = 0;
  150. // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
  151. // 将一次读取的字节数赋给byteread
  152. while ((byteread = randomFile.read(bytes)) != -1) {
  153. System.out.write(bytes, 0, byteread);
  154. }
  155. } catch (IOException e) {
  156. e.printStackTrace();
  157. } finally {
  158. if (randomFile != null) {
  159. try {
  160. randomFile.close();
  161. } catch (IOException e1) {
  162. }
  163. }
  164. }
  165. }

  166. /**
  167. * 显示输入流中还剩的字节数
  168. */
  169. private static void showAvailableBytes(InputStream in) {
  170. try {
  171. System.out.println("当前字节输入流中的字节数为:" + in.available());
  172. } catch (IOException e) {
  173. e.printStackTrace();
  174. }
  175. }

  176. public static void main(String[] args) {
  177. String fileName = "C:/temp/newTemp.txt";
  178. ReadFromFile.readFileByBytes(fileName);
  179. ReadFromFile.readFileByChars(fileName);
  180. ReadFromFile.readFileByLines(fileName);
  181. ReadFromFile.readFileByRandomAccess(fileName);
  182. }
  183. }
  184. 5、将内容追加到文件尾部
  185. public class AppendToFile {
  186. /**
  187. * A方法追加文件:使用RandomAccessFile
  188. */
  189. public static void appendMethodA(String fileName, String content) {
  190. try {
  191. // 打开一个随机访问文件流,按读写方式
  192. RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
  193. // 文件长度,字节数
  194. long fileLength = randomFile.length();
  195. //将写文件指针移到文件尾。
  196. randomFile.seek(fileLength);
  197. randomFile.writeBytes(content);
  198. randomFile.close();
  199. } catch (IOException e) {
  200. e.printStackTrace();
  201. }
  202. }

  203. /**
  204. * B方法追加文件:使用FileWriter
  205. */
  206. public static void appendMethodB(String fileName, String content) {
  207. try {
  208. //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
  209. FileWriter writer = new FileWriter(fileName, true);
  210. writer.write(content);
  211. writer.close();
  212. } catch (IOException e) {
  213. e.printStackTrace();
  214. }
  215. }

  216. public static void main(String[] args) {
  217. String fileName = "C:/temp/newTemp.txt";
  218. String content = "new append!";
  219. //按方法A追加文件
  220. AppendToFile.appendMethodA(fileName, content);
  221. AppendToFile.appendMethodA(fileName, "append end. \n");
  222. //显示文件内容
  223. ReadFromFile.readFileByLines(fileName);
  224. //按方法B追加文件
  225. AppendToFile.appendMethodB(fileName, content);
  226. AppendToFile.appendMethodB(fileName, "append end. \n");
  227. //显示文件内容
  228. ReadFromFile.readFileByLines(fileName);
  229. }
  230. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
神之梦 + 1

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马