栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Day - 19 -IO流

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Day - 19 -IO流

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

1.2分类

按处理数据类型: 分字节流和字符流

按数据流的不同,分为输入流和输出流

按功能:分为节点流: 直接操作数据源

处理流: 对其他流进行处理

1.3 四大抽象类: 字节输入流 InputStream 字节输出流 OutputStream

字符输入流 Reader 字符输出流 Writer

1.4 文件流

1.41 FileInputStream

概述 用来打开文件并读取文件中的数据, 根据位置找

1 绝对位置

以系统根目录为准,比如D:/xxx\xxxxxa.txt

2 相对位置

./ 表示当前目录

../ 表示上级目录

../../ 上上级目录

1.4.12  Read 使用        

Read: 读取一个字节,并返回对应的ASCII码值,返回为int 类型,如果文件读完了 返回-1

		//创建字节输入流
		//传入文件路径,用于打开该文件获取数据
		//绝对路径
		//FileInputStream fis = new FileInputStream("E:/a.txt");
		//在eclipse中,    ./ 定位当前项目,不是当前文件
		//相对路径
		try{
			FileInputStream fis = new FileInputStream("./src/IO_01/b.txt");
			//read : 读取一个字节,并返回对应的ASCII码值,返回为int 类型,如果到达文件末尾(读完了),返回-1
			int data = 0;
			while((data = fis.read()) != -1){
				System.out.println((char)data);
				}
			//关闭资源
		fis.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	

1.4.1.3 Read 重载使用

read 方法重载 :可以传递一个数组,一次读取会把该数组读满/ 读完,然后一次性返回

返回int 类型: 为当前读取的个数,如果达到文件末尾  返回-1

数组就相当于一个缓冲区,效率会有所提升

	public static void main(String[] args){
		try (FileInputStream fis =new FileInputStream("./src/IO_01/b.txt");) {
			//available :可读取的字节数
			System.out.println(fis.available());
			//数组容量并不是越大效率就越高,容量和大小刚好一致的时候,效率最高
			byte [] bytes = new byte[fis.available()];
			int temp = 0;
			while((temp = fis.read(bytes)) != -1){
				//把字节数组转换为字符串。可能出现数据重复问题
				System.out.println(new String(bytes));
				//因为read 返回的是当前次读取的元素个数,所以我们可以指定读多书,转换多少
				// 数组    起始位置(包含),个数
				System.out.print(new String(bytes,0,temp));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

1.4.2

FileReader: 一次读取一个字符,即俩个字节,主要用于读取纯文本文件,解决乱码问题

read() : 一次读取一个字符,返回对应的ASCII码,达到文件末尾返回-1

read(char[]) : 一次读取一个字符数组,提高读取效率,返回本地读取的字符个数,到达末尾返回-1

1.42.2s使用方式

public static void main(String[] args){
		try(FileReader fr = new FileReader("./src/IO_01/b.txt");){
			int temp = 0;
			while((temp = fr.read()) != -1){
				System.out.print((char)temp);
			}
			
		}catch (Exception e){
			e.printStackTrace();
		}
	}

1.43FileOutputStream

1.4.3.1   FileOutputStream   是字节输出流,用于把数据写出到指定文件中,如果指定文件不存在,会自动创建该文件,不会创建文件夹

1.4.32 使用方法

public static void main(String[] args){
		try{FileOutputStream fos = new FileOutputStream("D:/c.txt");
		//写出对应的ASCII码
		fos.write(98);
		byte [] bytes = {97,98,99};
		fos.write(bytes);
		
		//不能直接写出字符串
		String str ="你好";
		//getBytes : 把该字符串转换为字节数组
		fos.write(str.getBytes());
		//刷新缓存,强制持久化
		fos.flush();
		}catch(IOException e){
			e.printStackTrace();
		}
	}

1.4.4 FileWriter

public static void main(String[] args){
		//用法和字节输出一致,只不过新增了可以写出字符串的方法重载
		try(FileWriter fw =new FileWriter("./src/IO_01/b.txt")){
			//写出字符组
			char[] chars = {'x','z'};
			fw.write(chars);
			
			//可以直接写出字符串
			fw.write("你好吗");
			fw.flush();
		}catch(Exception e){
			e.printStackTrace();
		}

2缓冲流

2.1 字节输入缓冲流:BufferedInputStream,         字符输出缓冲流  BufferedReader

       字符输入缓冲流   BuffereOutputStream        字符输出缓冲流 BuffereWriter

public class BufferedReader_10 {
	public static void main(String[] args){
		try{
			//创建字符输入流
			FileReader fr = new FileReader("./src/IO_01/FileReader_06.java");
			//创建字符输入缓冲流对象
			BufferedReader by = new BufferedReader(fr);{
			//	by.readLine(); 
				String temp = null;
				while((temp = by.readLine()) != null){
				System.out.println(temp);
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}
	}
public class BufferWriter_11 {
	public static void main(String[] args){
		try{
			//创建字符输出流
			FileWriter fw = new FileWriter("./src/IO_01/b.txt");
			//创建字符输出缓冲流
			BufferedWriter bw = new BufferedWriter(fw);{
			//写出
				bw.write("你好吗今天");
				//换行
				bw.newline();
				bw.write("一般般");
				//刷缓存
				bw.flush();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
}

2.2转换流

输入流:InputStreamReader     输出流OutputStreamReader

public static void main(String[] args){
		try(FileInputStream fis = getInputStream("F:/p.txt");
			//使用转换流把字节输入流转换为字符输入流
				InputStreamReader isr = new InputStreamReader(fis);){
			int temp = 0;
			while ((temp = isr.read()) != -1) {
				System.out.print((char) temp);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		}
	// 需求:根据文件路径,获取文件的输入流对象
			public static FileInputStream getInputStream(String filePath) throws FileNotFoundException{
				return new FileInputStream(filePath);
	}
}

2.3 打印流

	public static void main(String[] args) throws Exception{
		//创建输出流
		FileOutputStream fos = new FileOutputStream("F:/p.txt");
		//封装为打印流
		PrintStream ps = new PrintStream(fos);
		ps.print(123);
		ps.print("风光过后");
		
		//System 中的打印流默认打印到控制它
		System.out.println("cxz");
		//更改System 中的打印流
		System.setOut(ps);
		
		//以下所有打印操作,打印到指定文件中,不是控制台
		System.out.println(123467);
		System.out.println("哈哈哈哈");
		
	}
}

2.4 数据流

数据流 是为了不同平台之间数据读取的统一性, Linux  Windows 等操作系统对数据进行存储的时候的方式是不同的,为了解决之间的差异性, java提供了 俩个轻量级的方法,只要每个平台有java环境,就能保证数据的一致性,比如在进行网络协议传输的时候,非常适用的

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/713609.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号