Java 学习 day21: IO流,字节(字符)输出输入流,字节(字符)缓冲输出输入流

1.IO

1.1        什么是IO

流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或者抽象,那数据在两个设备之间的传输成为流,流的本质就是数据传输,根据数据传输的特性将流抽象为各种类,方便更直观的进行数据操作

I就是input 输入:从硬盘中读取输入到内存中

O就是outpui 输出,从内存中读取输出到硬盘中

1.2        分类

按照处理数据类型的不同可以分为:        字节流和字符流

按照数据传入的方向不同可以分为:        输入流和输出流

按照功能的不同,分为节点流和处理流       

节点流就是直接操作数据源

处理流是对其他流(比如节点流)进行处理

1.3        四大抽象类

InputStream        字节输入

OutputStream        字节输出

Reader        字符输入

Writer        字符输出

2.InputStream        字节输入流

 

 2.1        read方法的使用方式

read:读取文件中的数据,一次读取一个字节,返回值为该字节的对应值(int类型),如果读取到了文件末尾,会返回-1

try(FileInputStream fis = new FileInputStream("./src/com/IO_01_FileInputStream_01.java") ;){
					int temp = 0 ;
					while((temp = fis.read()) != -1){
						System.out.print((char)temp);
					}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();

read的重载使用

可以传递一个字节数组,为了提高读取的效率

读取内容装满字节数组后,再进行下一次方法调用

read();         一次性读取一个字节数组,返回值为读取了一共读取的字符的个数,当读到文件末尾时返回-1

            try(FileInputStream fis = new         FileInputStream("./src/com/IO_01_FileInputStream_01.java");){
    		//available : 可获取的字节数,就是有多少个还没读的数量
			System.out.println(fis.available());
			//byte[] bytes = new byte[fis.available()] ;
			byte [] bytes = new byte[1025] ;
			int count = fis.read(bytes) ;
			System.out.println(new String(bytes,0,count));
			System.out.println(count);
			count = fis.read(bytes) ;
			
			//new String(bytes)把字节数组中的所有数据转化为字符串,但是可能有冗余的数据
			//所以推荐使用new String(bytes,0,count) ;
		}catch(Exception e){
			e.printStackTrace();
		}

3.Reader        字符输入流

 read();一次读取一个字符,返回值为该字符的int值,到达文件末尾返回值为-1

read(char[]) ; 一次读取一个字符数组,返回值为当前次读取个数,到达末尾返回值为-1 

try(FileReader fr = new FileReader("./src/com/IO_01_FileInputStream_01.java")){
				int temp = 0 ;
				while((temp = fr.read())!=-1){
					System.out.print((char)temp);
				}
			}catch(Exception e){
				e.getStackTrace() ;
			}

重载传入char数组

 4.OutputStream        字节输出流

 输出流是吧内存中的数据写到硬盘中,如果改文件不存在就会自动创建,但是不会创建文件夹

如果对应的文件夹不存在就报错

构造方法

FileOutputStream(String path) ; 向文件中输出数据,并覆盖原有的内容

FileOutputStream(String path,boolean append) ;true就追加内容,false就覆盖内容

常用方法

write(int);写出一个数据

write(byte[]);写出这个字节数组中所有的数据

write(byte[],int,int) ; 写出这个字节数组中指定的数据

flush();刷缓存,关闭资源的时候会自动调用

字符串的话可以利用   辅助方法:getBytes();String中的方法,用于获取字符串对应的字节数组

使用方式

try(FileOutputStream fos = new FileOutputStream("D:/a.txt") ;
){			fos.write(97) ;
fos.write(97) ;
fos.write(97) ;
fos.write(97) ;
fos.write(97) ;
fos.write('\n') ;
				String str = "你好吗" ;
				byte[] bytes =str.getBytes() ;
				fos.write(bytes);
				fos.flush() ;
			}catch(Exception e){
				e.printStackTrace();
			}

4.Write        字符输出流

4.1        什么是字符输出流

 4.2        使用方式

            try(FileWriter fw = new FileWriter("D:/a.txt");){
			//写出字符串
			fw.write("你好吗?") ;
			char[] chars = {'a','v','c'} ;
			//输出字符数组
			fw.write(chars);
			fw.flush();
			}catch(Exception e){
				e.printStackTrace();
				
			}

5.缓冲流

5.1        什么是缓冲流

 5.2        BufferedInputStream

        

            long startTime = System.currentTimeMillis() ;
			try(FileInputStream fis = new FileInputStream("D:/a.txt");
					BufferedInputStream bis = new BufferedInputStream(fis);){
				byte [] bytes = new byte[1024] ;
				int temp = 0 ;
				while((temp = bis.read(bytes))!=-1){
				}
				long endTime = System.currentTimeMillis() ;
				
			}catch(Exception e){
				e.printStackTrace();
			}

5.3        BufferedOutputStream

            try(FileOutputStream fos = new FileOutputStream("D:/a.txt");
					BufferedOutputStream bos = new BufferedOutputStream(fos);){
					bos.write(97);
					bos.write(97);
					bos.write(97);
					bos.write(97);
					bos.write(97);
					String str = "你好吗";
					byte[] bytes = str.getBytes();
					bos.write(bytes);
					bos.flush();
			}catch(Exception e){
				e.printStackTrace();
				
			}

5.4        BufferedReader

        try (FileReader fr = new FileReader("D:/a.txt");
				BufferedReader br = new BufferedReader(fr);) {
			String temp = null;
			// 读取一行
			while ((temp = br.readLine())  != null) {
				System.out.println(temp);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

5.5       BufferedWriter

        BufferedWriter bw = null ;
		try {
			 bw = new BufferedWriter(new FileWriter("D:/a.txt"));

			bw.write("你好吗");
			// 换行
			bw.newLine();
			bw.write("我很好");
			bw.flush();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (bw != null) {
					bw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值