java读取方法大全.doc

上传人:hs****ma 文档编号:544642070 上传时间:2023-10-10 格式:DOC 页数:35 大小:93.50KB
返回 下载 相关 举报
java读取方法大全.doc_第1页
第1页 / 共35页
java读取方法大全.doc_第2页
第2页 / 共35页
java读取方法大全.doc_第3页
第3页 / 共35页
java读取方法大全.doc_第4页
第4页 / 共35页
java读取方法大全.doc_第5页
第5页 / 共35页
点击查看更多>>
资源描述

《java读取方法大全.doc》由会员分享,可在线阅读,更多相关《java读取方法大全.doc(35页珍藏版)》请在金锄头文库上搜索。

1、java读取文件方法大全一、多种方式读文件内容。 1、按字节读取文件内容 2、按字符读取文件内容 3、按行读取文件内容 4、随机读取文件内容 Java代码 1.import java.io.BufferedReader; 2.import java.io.File; 3.import java.io.FileInputStream; 4.import java.io.FileReader; 5.import java.io.IOException; 6.import java.io.InputStream; 7.import java.io.InputStreamReader; 8.impor

2、t java.io.RandomAccessFile; 9.import java.io.Reader; 10. 11.public class ReadFromFile 12. /* 13. * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 14. * 15. * param fileName 16. * 文件的名 17. */ 18. public static void readFileByBytes(String fileName) 19. File file = new File(fileName); 20. InputStream in = null; 21

3、. try 22. System.out.println(以字节为单位读取文件内容,一次读一个字节:); 23. / 一次读一个字节 24. in = new FileInputStream(file); 25. int tempbyte; 26. while (tempbyte = in.read() != -1) 27. System.out.write(tempbyte); 28. 29. in.close(); 30. catch (IOException e) 31. e.printStackTrace(); 32. return; 33. 34. try 35. System.ou

4、t.println(以字节为单位读取文件内容,一次读多个字节:); 36. / 一次读多个字节 37. byte tempbytes = new byte100; 38. int byteread = 0; 39. in = new FileInputStream(fileName); 40. ReadFromFile.showAvailableBytes(in); 41. / 读入多个字节到字节数组中,byteread为一次读入的字节数 42. while (byteread = in.read(tempbytes) != -1) 43. System.out.write(tempbytes

5、, 0, byteread); 44. 45. catch (Exception e1) 46. e1.printStackTrace(); 47. finally 48. if (in != null) 49. try 50. in.close(); 51. catch (IOException e1) 52. 53. 54. 55. 56. 57. /* 58. * 以字符为单位读取文件,常用于读文本,数字等类型的文件 59. * 60. * param fileName 61. * 文件名 62. */ 63. public static void readFileByChars(Str

6、ing fileName) 64. File file = new File(fileName); 65. Reader reader = null; 66. try 67. System.out.println(以字符为单位读取文件内容,一次读一个字节:); 68. / 一次读一个字符 69. reader = new InputStreamReader(new FileInputStream(file); 70. int tempchar; 71. while (tempchar = reader.read() != -1) 72. / 对于windows下,rn这两个字符在一起时,表示一

7、个换行。 73. / 但如果这两个字符分开显示时,会换两次行。 74. / 因此,屏蔽掉r,或者屏蔽n。否则,将会多出很多空行。 75. if (char) tempchar) != r) 76. System.out.print(char) tempchar); 77. 78. 79. reader.close(); 80. catch (Exception e) 81. e.printStackTrace(); 82. 83. try 84. System.out.println(以字符为单位读取文件内容,一次读多个字节:); 85. / 一次读多个字符 86. char tempchar

8、s = new char30; 87. int charread = 0; 88. reader = new InputStreamReader(new FileInputStream(fileName); 89. / 读入多个字符到字符数组中,charread为一次读取字符数 90. while (charread = reader.read(tempchars) != -1) 91. / 同样屏蔽掉r不显示 92. if (charread = tempchars.length) 93. & (tempcharstempchars.length - 1 != r) 94. System.o

9、ut.print(tempchars); 95. else 96. for (int i = 0; i charread; i+) 97. if (tempcharsi = r) 98. continue; 99. else 100. System.out.print(tempcharsi); 101. 102. 103. 104. 105. 106. catch (Exception e1) 107. e1.printStackTrace(); 108. finally 109. if (reader != null) 110. try 111. reader.close(); 112. c

10、atch (IOException e1) 113. 114. 115. 116. 117. 118. /* 119. * 以行为单位读取文件,常用于读面向行的格式化文件 120. * 121. * param fileName 122. * 文件名 123. */ 124. public static void readFileByLines(String fileName) 125. File file = new File(fileName); 126. BufferedReader reader = null; 127. try 128. System.out.println(以行为单位读取文件内容,一次读一整行:); 129. reader = new BufferedReader(n

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号