博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IO流(字节流和字符流)
阅读量:3965 次
发布时间:2019-05-24

本文共 5372 字,大约阅读时间需要 17 分钟。

IO流的分类:

  • 按照数据流向 站在内存角度
    输入流 读入数据
    输出流 写出数据
  • 按照数据类型
    字节流 可以读写任何类型的文件 比如音频 视频 文本文件
    字符流 只能读写文本文件

1.字节输入流的继承体系(常用的)

在这里插入图片描述

InputStream的功能 概述
read(byte[] b, int off, int len) 从流中从off的位置开始读取len个字节的数据存储到b中,返回结果是实际读取到的字节个数(当再次读时,如果返回-1说明到了结尾,没有了数据)
int read(byte[] b) 一次读取一个字节数组
Close() 关闭流,释放资源

(1)FileInputStream流

其特有的方法 概述
read() 一次读取一个字节
public class MyTest2 {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("a.txt"); //创建一个空的字节数组,充当缓冲区 byte[] bytes = new byte[1024]; //返回值是他读取到的有效的字节个数 int len = in.read(bytes); for (byte aByte : bytes) {
System.out.println(aByte); } System.out.println(len); String s = new String(bytes,0,len); System.out.println(s); //释放资源 in.close(); }}

(2)ByteArrayInputStream内存操作流

private static void read(byte[] bytes) throws IOException {
/* ByteArrayInputStream( byte[] buf) 创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组。*/ ByteArrayInputStream bis = new ByteArrayInputStream(bytes); byte[] bytes1 = {
(byte) read, (byte) read1, (byte) read2}; String s = new String(bytes1); System.out.println(s); 注意:内存操作流:这个流不关联任何文件,只能在内存中数据

(3)ObjectInputStream 反序列化流 读取对象

对对象进行读取

private static void readObj() throws IOException, ClassNotFoundException {
ObjectInputStream objIN = new ObjectInputStream(new FileInputStream("student.txt")); Object o = objIN.readObject(); Student student= (Student) o;//转型 System.out.println(student.getName()); System.out.println(student.getAge());

2.字节输出流的继承体系(常用的)

在这里插入图片描述

OutputStream类的方法 概述
write(byte[] b) 将b的长度个字节数据写到输出流中。
write(byte[] b,int off,int len) 从b的off位置开始,获取len个字节数据,写到输出流中。
flush() 刷新输出流,把数据马上写到输出流中。
close() 关闭流释放资源

(1)FileOutputStream流

public class MyTest2 {
public static void main(String[] args) throws IOException {
//默认的是,你每次运行,都会覆盖之前文件中的数据 /* FileOutputStream(File file, boolean append) 创建一个向指定 File 对象表示的文件中写入数据的文件输出流。 FileOutputStream(String name, boolean append) 创建一个向具有指定 name 的文件中写入数据的输出文件流。 */ //参数2:true 代表追加写入,不会重新覆盖文件中之前的数据 FileOutputStream out = new FileOutputStream("e.txt",true); out.write("江畔何人初见月".getBytes()); out.write("\r\n".getBytes()); out.write("江月何年初照人".getBytes()); out.write("\r\n".getBytes()); out.close(); }}

(2)ByteArrayOutputStream内存操作流

ByteArrayOutputStream bos = new ByteArrayOutputStream();  bos.write("好好学习".getBytes());  bos.write("天天向上".getBytes());  bos.write("爱生活".getBytes());  bos.write("爱Java".getBytes());  byte[] bytes = bos.toByteArray();  read(bytes);

(3)ObjectOutputStream序列化流 存储对象

private static void wiriteObj() throws IOException {
Student student = new Student("张三", 23);ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream("student.txt")); objOut.writeObject(student); objOut.close(); }}

3.字符输入流的继承体系

在这里插入图片描述

Reader流的方法 概述
read() 读取单个字符,返回结果是一个int,需要转成char;到达流的末尾时,返回-1
read(char[] c) 读取c的长度个字符,返回结果是读取的字符数
close() 关闭流,释放占用的系统资源。

(1)InputStreamReader

其特有的方法 概述
read(char[] cbuf, int offset, int length) 从offset位置开始,读取length个字符到cbuf中,返回结果是实际读取的字符数,到达流的末尾时,返回-1
InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt"));char[] chars = new char[1024];int len = in.read(chars); //返回值是读取到的有效字符个数//把字符数组转换成字符串String s = new String(chars, 0, len);System.out.println(s);

(2)BufferedReader

其特有的方法 概述
read(char[] cbuf, int offset, int length) 从offset位置开始,读取length个字符到cbuf中,返回结果是实际读取的字符数,到达流的末尾时,返回-1
readLine() 读取一个文本行,以行结束符作为末尾,返回结果是读取的字符串。如果已到达流末尾,则返回 null
public class MyTest2 {
public static void main(String[] args) throws IOException {
/* A: 案例演示: 需求:从文本文件中读取数据(每一行为一个字符串数据) 到集合中,并遍历集合*/ BufferedReader bfr = new BufferedReader(new FileReader("name.txt")); ArrayList
list = new ArrayList<>(); while (true) {
String s = bfr.readLine(); if (s == null) {
break; } list.add(s.trim()); } System.out.println(list); }}

4.字符输出流的继承体系

在这里插入图片描述

Writer流的方法 概述
write(char[] cbuf) 往输出流写入一个字符数组。
write(int c) 往输出流写入一个字符。
write(String str) 往输出流写入一串字符串
write(String str, int off, int len) 往输出流写入字符串的一部分。
close() 关闭流,释放资源。
flush() 刷新输出流,把数据马上写到输出流中。

(1)OutputStreamWriter

public class MyTest2 {
public static void main(String[] args) throws IOException {
//参数2 可以指定码表 GBK UTF-8 OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("b.txt",true),"UTF-8"); out.write("我们使用GBK编码写入"); out.write("\r\n"); out.write("我们使用GBK编码写入"); out.write("\r\n"); out.flush();//字符流记得刷新 //释放资源 out.close(); }}

(2)BufferedWriter

其特有的方法 概述
newLine() 写 入一个换行符
public class MyTest {
public static void main(String[] args) throws IOException {
/*A: 案例演示: 需求:把ArrayList集合中的字符串数据存储到文本文件*/ ArrayList
list = new ArrayList<>(); list.add("张飞"); list.add("赵云"); list.add("马超"); list.add("关羽"); list.add("黄忠"); list.add("邢道荣"); BufferedWriter bfw = new BufferedWriter(new FileWriter("name.txt")); for (String s : list) {
bfw.write(s); bfw.newLine(); bfw.flush(); } bfw.close(); }}

转载地址:http://ydfki.baihongyu.com/

你可能感兴趣的文章
常用STL算法3_排序
查看>>
常用STL算法4_拷贝和替换
查看>>
STL综合案例
查看>>
O(logn)时间复杂度求Fibonacci数列
查看>>
【转】腾讯十年运维老兵:运维团队的五个“杀手锏”
查看>>
Iterator_traits
查看>>
Zedboard中的SPI通信记录文档(已实现)
查看>>
Android 发布到google Play的app搜索不到问题的解决
查看>>
Flutter 网络请求之基于dio的简单封装
查看>>
Flutter UI基础 - 路由之Navigator详解
查看>>
Flutter UI基础 - Widgets 之 InkWell 和 Ink
查看>>
Spring - sentinel和hystrix比较
查看>>
Flutter 日期插件date_format 中文 国际化 及flutter_cupertino_date_picker
查看>>
Flutter 插件笔记 | 屏幕适配 flutter_screenutil
查看>>
Flutter UI基础 - 侧拉抽屉菜单
查看>>
Flutter UI基础 - AppBar中标题文字如何居中
查看>>
Flutter UI基础 - Drawer 抽屉视图与自定义header
查看>>
Flutter UI基础 - GridView
查看>>
Flutter UI基础 - 使用InkWell给任意Widget添加点击事件
查看>>
OC WKWebView的使用
查看>>