2022年java读取文件大全 .pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《2022年java读取文件大全 .pdf》由会员分享,可在线阅读,更多相关《2022年java读取文件大全 .pdf(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、还不错建议看下. publicclass ReadFromFile /* * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 */publicstaticvoid readFileByBytes(String fileName) File file = new File(fileName); InputStream in = null; try System.out.println(以字节为单位读取文件内容,一次读一个字节:); / 一次读一个字节 in = new FileInputStream(file); int tempbyte; while (tempbyte
2、= in.read() != -1) System.out.write(tempbyte); in.close(); catch (IOException e) e.printStackTrace(); return; try System.out.println(以字节为单位读取文件内容,一次读多个字节:); / 一次读多个字节byte tempbytes = newbyte100; int byteread = 0; in = new FileInputStream(fileName); ReadFromFile.showAvailableBytes(in); / 读入多个字节到字节数组中
3、,byteread为一次读入的字节数while (byteread = in.read(tempbytes) != -1) System.out.write(tempbytes, 0, byteread); catch (Exception e1) e1.printStackTrace(); finally if (in != null) try 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 7 页 - - - - - - - - - in.close(); catch
4、 (IOException e1) /* * 以字符为单位读取文件,常用于读文本,数字等类型的文件 */publicstaticvoid readFileByChars(String fileName) File file = new File(fileName); Reader reader = null; try System.out.println(以字符为单位读取文件内容,一次读一个字节:); / 一次读一个字符 reader = new InputStreamReader(new FileInputStream(file);int tempchar; while (tempchar
5、= reader.read() != -1) / 对于 windows下, rn这两个字符在一起时,表示一个换行。 / 但如果这两个字符分开显示时,会换两次行。 / 因此,屏蔽掉 r,或者屏蔽 n 。否则,将会多出很多空行。if (char ) tempchar) != r) System.out.print(char) tempchar); reader.close(); catch (Exception e) e.printStackTrace(); try System.out.println(以字符为单位读取文件内容,一次读多个字节:); / 一次读多个字符char tempchars
6、 = newchar30; int charread = 0; reader = new InputStreamReader(new FileInputStream(fileName); / 读入多个字符到字符数组中,charread为一次读取字符数名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 7 页 - - - - - - - - - while (charread = reader.read(tempchars) != -1) / 同样屏蔽掉 r不显示if (cha
7、rread = tempchars.length) & (tempcharstempchars.length - 1 != r) System.out.print(tempchars); else for (int i = 0; i 4) ? 4 : 0; / 将读文件的开始位置移到beginIndex位置。 randomFile.seek(beginIndex); byte bytes = newbyte10; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 7 页 -
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年java读取文件大全 2022 java 读取 文件 大全
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内