InputStream输入流和OutputStream输出流

一、InputStream字节输入流

InputStream是一个抽象类,所有继承了InputStream的类都是字节输入流

InputStream有三个子类:FileInputStream(文件输入流)、BufferedInputStream(缓存输入流)、ObjectInputStream(对象输入流)

InputStream这个抽象类定义的最重要的方法就是int read(),这个方法会读取输入流的下一个字节,并返回字节表示的int值(0-255),如果已读到末尾,返回-1表示不能继续读取了。

1、FileInputStream(文件输入流)

注意事项:传入的路径一定是一个具体的文件,不能是目录(报异常)

public class Demo01 {
	public static void main(String[] args) throws FileNotFoundException {
		构造文件输入流的两个方法:

        1、传入File对象
        File file = new File("E:\\javaee2307IO\\apsource\\a.txt");
        FileInputStream stream = new FileInputStream(file);
        
        2、传入一个String类型的路径(FileInputStream(String name))
        FileInputStream fst = new  FileInputStream("E:\\javaee2307IO\\apsource\\a.txt");
	}
}

主要方法:

void

close()  

关闭此输入流并释放与该流关联的所有系统资源。

abstract int

read() 

从输入流读取下一个数据字节。(多次调用会按顺序依次读取)返回-1表示读取完毕

intread(byte[] b)
从输入流中读取一定数量的字节并将其存储在缓冲区数组 b 中。
intread(byte[] b, int off, int len)
将输入流中最多 len 个数据字节读入字节数组。

2、缓冲读取

利用缓冲区一次性读取多个字节效率往往要高很多,InputStream提供了两个重载方法来支持读取多个字节:

int read(byte[] b) : 读取若干个字节并填充到byte[]数组,返回读取的字节数

int read(byte[] b,int off,int len):指定byte[]数组的偏移量和最大填充数

public class Demo03 {
	public static void main(String[] args) {

		try(FileInputStream fts = new FileInputStream("E:\\javaee2307IO\\apsource\\b.txt")){
			byte[] butter = new byte[5];
			int n;
			while((n = fts.read(butter)) != -1) {
				System.out.println("长度为" + n + "内容为" + new String(butter,0,n));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}

3、BufferedInputStream(缓冲输入流)

BufferedInputStream是缓冲输入流,它继承于FilterInputStream(过滤输入流),缓冲输入流的作用是为另一个输入流添加一些功能

public class Demo04 {
	public static void main(String[] args) {
		try(FileInputStream fts = new FileInputStream("E:\\javaee2307IO\\apsource\\b.txt");
			BufferedInputStream buff = new BufferedInputStream(fts);){
			byte[] but = new byte[5];
			int a = -1;
			while((a = buff.read(but)) != -1) {
				System.out.println("长度为" + a + "内容为" + new String(but,0,a));
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

二、OutputStream字节输出流

OutputStream也是一个抽象类,它是用于写入操作的基础输出流,它是所有输出流的超类。这个抽象类定义的一个重要的方法是void write(int b)

这个方法会写入到一个字节到输出流,需要注意的是,虽然传入的是int参数,单只会写入一个字节,即只写入int最低8位表示字节的部分。

OutputStream有三个子类:FileOutputStream(文件输出流)、BufferedOutputStream(缓存输出流)、ObjectOutputStream(对象输出流)

1、FileOutputStream文件输出流

注意事项:(1)路径一定要存在,文件不存在,会自动创建一个文件

                  (2)传入路径一定要是文件,不可以是目录

public class Demo05 {
	public static void main(String[] args) throws FileNotFoundException {

        // 构造方法1
        // 传入一个file对象
		File f = new File("E:\\javaee2307IO\\apsource\\c.txt");
		FileOutputStream fous = new FileOutputStream(f);
		
        // 构造方法2
        // 传入一个String类型的路径
		FileOutputStream fs = new FileOutputStream("E:\\javaee2307IO\\apsource\\da.txt");
	}
}

主要方法:

voidclose()
关闭此输出流并释放与此流有关的所有系统资源。
voidflush()
刷新此输出流并强制写出所有缓冲的输出字节。
voidwrite(byte[] b)
将 b.length 个字节从指定的字节数组写入此输出流。
voidwrite(byte[] b, int off, int len)
将指定字节数组中从偏移量 off 开始的 len 个字节写入此输出流。
abstract voidwrite(int b)
将指定的字节写入此输出流,每次写出一个字节数据
public class Demo07 {
	public static void main(String[] args) {
        // 方式一:FileOutputStream(String path,boolean append)
        // 传参1:是一个String类型的路径
        // 参数2:boolean类型的值,true源文件进行追加,false清空原文件重写内容
        
        // 方式二:FileOutputStream(File path,boolean append)
        // 传参1:是一个file对象
        // 参数2:boolean类型的值,true原文件进行追加,false清空原文件重写内容

		String path = "E:\\javaee2307IO\\apsource\\b.txt";
		
		try(FileOutputStream fop = new FileOutputStream(path,true);){
			//从系统中获取分隔符
			String b = System.lineSeparator();
            //字符串转化为字节数组
			byte[] a = ("去留肝胆两昆仑"+b).getBytes();
            //写出字节数组中的内容
			fop.write(a);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

2、BufferedOutputStream缓存输出流

缓存输出流里面包含了一个8192字节的缓冲区,new这个构造函数默认分配的就是8192个字节的byte[ ]数组(即缓冲区)

public class Demo08 {
	public static void main(String[] args) {
		String path = "E:\\javaee2307IO\\apsource\\b.txt";
		
		try (BufferedOutputStream bop = new BufferedOutputStream(new FileOutputStream(path,true));){
			byte[] b = "索里亚给同".getBytes();
			bop.write(b);			
			String str = System.lineSeparator();
			byte[] b1 = ("疾风与我同在"+str).getBytes();
			bop.write(b1);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

缓存输出流的使用步骤:

1、创建FileOutputStream字节输出流对象,构造方法中绑定要输出的目标文件。

2、创建BufferedOutputStream对象,构造方法中传递FileOutputStream对象,提高FileOutputStream的写入效率。

3、使用BufferedOutputStream对象中的方法write()方法,把数据写入到内部缓冲区中。

4、使用BufferedOutputStream对象中的flush()方法,把内部缓冲区的数据,刷新到文件中。

5、使用BufferedOutputStream对象中的close()方法,释放资源(会先调用flush()方法刷新数据)。

  • 7
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值