字节流中的 read() 方法解析

网友投稿 699 2022-11-05

字节流中的 read() 方法解析

字节流中的 read() 方法解析

package com.yqq.app6;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;/** * @Author yqq * @Date 2021/11/11 19:58 * @Version 1.0 */public class FirstDemo { public static void main(String[] args) { FileInputStream fis = null; //创建字节输入流对象 try { fis = new FileInputStream("D:/var/wc.txt"); //read() 一次只能读取一个字符的ascii码 int s1 = fis.read();//打印输入第一个字符对应的ASCII码 int s2 = fis.read();//打印输入第二个字符对应的ASCII码 System.out.println(s1+"-"+s2); } catch (Exception e) { e.printStackTrace(); }finally { if(fis!=null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } }}

package com.yqq.app6;import java.io.FileInputStream;import java.io.IOException;/** * @Author yqq * @Date 2021/11/11 20:09 * @Version 1.0 */public class SecondDemo { public static void main(String[] args) { FileInputStream fis = null; try { //创建字节输入流对象 fis = new FileInputStream("D:/var/wc.txt"); StringBuilder sb = new StringBuilder(); int temp = 0; while((temp = fis.read())!=-1){ System.out.println(temp); sb.append((char) temp); } System.out.println(sb.toString()); }catch (Exception e){ e.printStackTrace(); }finally { if(fis!=null){ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Thunder-Mail是一个开源程序,用于尽可能便宜地使用API发送数百万封电子邮件
下一篇:一个Android帮助程序类,用于使用应用的raw asset来管理数据库创建和版本管理
相关文章

 发表评论

暂时没有评论,来抢沙发吧~