IO流:字节流的读写

阅读: 评论:0

IO流:字节流的读写

IO流:字节流的读写

【字节流的写入方式】:

这里的close()只有关闭资源的作用,没有刷新作用。因为字节单位已经很小了,不需要经过中间处理,直接存入内存,所以不用刷新。 

public static void writeFile() throws IOException{FileOutputStream fos=new FileOutputStream(&#");fos.write("abcde".getBytes());fos.close();
}

【字节流的读取方式】:

方式一:一个字节一个字节的读取。

方式二:利用缓冲数组存储起来,一次性打印。(数组长度为自定义)

方式三:利用缓冲数组存储起来,一次性打印。(数组长度为文件字节数)

/*方式一*/
public static void readFile_1() throws IOException{FileInputStream fis=new FileInputStream(&#");int ch=0;while((ch&#ad())!=-1){System.out.println((char)ch);}fis.close();
}/*方式二*/
public static void readFile_2() throws IOException{FileInputStream fis=new FileInputStream(&#");byte[] buf=new byte[1024];int len=0;while((len&#ad(buf))!=-1){System.out.println(new String(buf,0,len));}fis.close();
}/*方式三*/
public static void readFile_3() throws IOException{FileInputStream fis=new FileInputStream(&#");int len=fis.available();byte[] buf=new byte[len];ad(buf);System.out.println(new String(buf));fis.close();
}

【示例】:

读取文件:

【输出】:

方式一对应:

 

方式二对应: 

方式三对应:

 

本文发布于:2024-01-29 02:15:02,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170646570512003.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:字节   IO
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23