【字节流的写入方式】:
这里的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((chad())!=-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((lenad(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小时内删除。
留言与评论(共有 0 条评论) |